The shortest IE judgment method, the original address: http://www.jb51.net/article/23621.htm
The shortest Method for judging ie browsers: var isIE =! -[1,]
Principle: [1,] "1" is returned in the standard browser, which is equivalent to calling [1,]. toString () and returning "1," in IE ,"
When a negative number is forcibly converted to a number for the returned value, the standard browser returns 1, and IE returns NaN,
When the first and NaN values are reversed, the standard browser returns false, and IE returns true.
Js determines the browser type
There is basically no problem with the original article, but his judgment method is not very rigorous.
For example, navigator. userAgent. indexOf ("Safari")> 0. If the word "Safari" appears at the frontend of userAgent, the correct result cannot be obtained. Change> 0! =-1
Then I tested the existing five independent kernel browsers on my computer and found that the userAgent value of Opera was "Opera/9.80 (Windows NT 5.1; U; Ed ..."
I just confirmed what I just said.
In fact, most of the time we make browser compatibility mainly for IE and non-IE browsers, that is to say, we only need to determine whether it is an IE browser.
Judge the current browser type code
Copy codeThe Code 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>