Js Code to determine the browser type IE, FF, Opera, Safari, chrome, and version _ javascript skills

Source: Internet
Author: User
This article mainly shares the judgment IE, FF, Opera, Safari, Chrome and other browsers and versions of the two methods, need friends can refer to the version of the ie10-ie11 because of the problem, no longer support document. all judgment, so the ie judgment function should be rewritten.

function isIE() { //ie?      if (!!window.ActiveXObject || "ActiveXObject" in window)        return true;      else        return false;    }

First, only the browser is differentiated, regardless of the version

The Code is as follows:


Function myBrowser (){
Var userAgent = navigator. userAgent; // obtain the userAgent string of the browser.
Var isOpera = userAgent. indexOf ("Opera")>-1;
If (isOpera ){
Return "Opera"
}; // Determine whether the operabrowser is used
If (userAgent. indexOf ("Firefox")>-1 ){
Return "FF ";
} // Determine if the Firefox browser is used
If (userAgent. indexOf ("Chrome")>-1 ){
Return "Chrome ";
}
If (userAgent. indexOf ("Safari")>-1 ){
Return "Safari ";
} // Determine whether the browser is Safari
If (userAgent. indexOf ("compatible")>-1 & userAgent. indexOf ("MSIE")>-1 &&! IsOpera ){
Return "IE ";
}; // Determines whether the IE browser is used
}

// The following is a call to the above function
Var mb = myBrowser ();
If ("IE" = mb ){
Alert ("I am IE ");
}
If ("FF" = mb ){
Alert ("I am Firefox ");
}
If ("Chrome" = mb ){
Alert ("Chrome ");
}
If ("Opera" = mb ){
Alert ("My name is Opera ");
}
If ("Safari" = mb ){
Alert ("I am Safari ");
}

Second, differentiate browsers and consider IE5.5 6 7 8

The Code is as follows:


Function myBrowser (){
Var userAgent = navigator. userAgent; // obtain the userAgent string of the browser.
Var isOpera = userAgent. indexOf ("Opera")>-1; // determines whether the operabrowser is used.
Var isIE = userAgent. indexOf ("compatible")>-1 & userAgent. indexOf ("MSIE")>-1 &&! IsOpera; // determines whether the IE browser is used
Var isFF = userAgent. indexOf ("Firefox")>-1; // determine whether the Firefox browser is used
Var isSafari = userAgent. indexOf ("Safari")>-1; // determine whether the browser is Safari
If (isIE ){
Var IE5 = IE55 = IE6 = IE7 = IE8 = false;
Var reIE = new RegExp ("MSIE (\ d + \. \ d + );");
ReIE. test (userAgent );
Var fIEVersion = parseFloat (RegExp ["$1"]);
IE55 = fIEVersion = 5.5;
IE6 = fIEVersion = 6.0;
IE7 = fIEVersion = 7.0;
IE 8 = fIEVersion = 8.0;
If (IE55 ){
Return "IE55 ";
}
If (IE6 ){
Return "IE6 ";
}
If (IE7 ){
Return "IE7 ";
}
If (IE8 ){
Return "IE8 ";
}
} // IsIE end
If (isFF ){
Return "FF ";
}
If (isOpera ){
Return "Opera ";
}
} // MyBrowser () end
// The following is a call to the above function
If (myBrowser () = "FF "){
Alert ("I am Firefox ");
}
If (myBrowser () = "Opera "){
Alert ("My name is Opera ");
}
If (myBrowser () = "Safari "){
Alert ("I am Safari ");
}
If (myBrowser () = "IE55 "){
Alert ("I'm IE5.5 ");
}
If (myBrowser () = "IE6 "){
Alert ("I'm IE6 ");
}
If (myBrowser () = "IE7 "){
Alert ("I am IE7 ");
}
If (myBrowser () = "IE8 "){
Alert ("I am IE8 ");
}

The following is a javascript code used to determine whether the browser is IE.

The principle is based on the difference between IE and the standard browser in handling the toString method of arrays. For the standard browser, if the last character in the array is a comma, The JS engine will automatically remove it.

Script var ie =! -[1,]; alert (ie); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Related Article

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.