Js determines the current browser type and IE browser Method

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.