Home > Web Front-end > JS Tutorial > js method to determine browser type_javascript skills

js method to determine browser type_javascript skills

WBOY
Release: 2016-05-16 17:26:32
Original
1054 people have browsed it

The code is as follows:

Copy code The code is as follows:

var isIE=!!window.ActiveXObject;
var isIE6=isIE&&!window.XMLHttpRequest;
var isIE8=isIE&&!!document.documentMode;
var isIE7=isIE&&!isIE6&&!isIE8;
if (isIE){
if (isIE6){
            alert(”ie6″);
}
}


The above is a commonly used JavaScript code to detect which version of IE the browser is.

In January this year, a Russian took advantage of the difference between IE and standard browsers in the toString method of processing arrays, and perfectly used 6bytes:!-[1,] to complete the detection of whether it is an IE browser. . The previous shortest code was: ! 7bytes of "v1". Using these findings we can also make our code shorter and more artistic. Nowadays, detecting whether it is IE6 can actually be written as

!-[1,]&&!window.XMLHttpRequest

This code is quite useful in actual use. Since it can be written shorter, why not do it. Compared with the previous cumbersome analysis of the navigator and then the regular comparison method, it is certainly more efficient, and the code is more concise. In the process of writing JavaScript, you should make more use of operators such as ||, &&, !, !!, not just in cases like if().

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template