Shortest IE judgment method, original address: //www.VeVB.COM/article/23621.htm
How to judge the shortest IE browser: var isIE = !-[1,]
Principle: [1,] returns "1" in standard browser, which is equivalent to calling [1,].toString(), and returns "1," in IE
When using a negative sign to a number for the return value, the standard browser returns 1, and IE returns NaN.
When 1 and NaN are inversely redirected, the standard browser returns false and IE returns true.
JS determines browser type
There is basically no problem with the original text, but his judgment method is not very rigorous.
For example: navigator.userAgent.indexOf("Safari")>0, if the word "Safari" appears at the front end of userAgent, it is not impossible to get the correct result. It should be changed to !=-1
Then I tested the existing 5 independent kernel browsers on my machine and found that the userAgent value of Opera is "Opera/9.80 (Windows NT 5.1; U; Ed..."
It just confirms what I just said.
In fact, most of the time when we are doing browser compatibility, we mainly target IE and non-IE browsers, which means that in general, we only need to determine whether it is an IE browser.
Determine the current browser type code
The code copy is as follows:
<script type="text/javascript" >
<!--
function getOs()
{
var OsObject = "";
if(isIE = navigator.userAgent.indexOf("MSIE")!=-1) {
return "MSIE";
}
if(isFirefox=navigator.userAgent.indexOf("Firefox")!=-1){
return "Firefox";
}
if(isChrome=navigator.userAgent.indexOf("Chrome")!=-1){
return "Chrome";
}
if(isSafari=navigator.userAgent.indexOf("Safari")!=-1) {
return "Safari";
}
if(isOpera=navigator.userAgent.indexOf("Opera")!=-1){
return "Opera";
}
}
alert("type -> "+getOs());
-->
</script>