JS code to determine browser type IE, FF, Opera, Safari, Chrome and version

Source: Internet
Author: User

This article mainly shared the judgment of IE, FF, Opera, Safari, Chrome and other browser and version of the two methods, the need for friends can refer to the following

Because IE10-IE11 version problem, no longer support document.all judgment, so IE judgment function to re-write

?
123456 function isie () { //ie?        if (!! Window. ActiveXObject | | "ActiveXObject" in window)          return true        else          return false     

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

Copy CodeThe code is as follows:
function Mybrowser () {
var useragent = navigator.useragent; Get the useragent string for the browser
var Isopera = Useragent.indexof ("Opera") >-1;
if (Isopera) {
Return "Opera"
}; Determine if Opera browser
if (Useragent.indexof ("Firefox") >-1) {
return "FF";
}//Determine if Firefox browser
if (Useragent.indexof ("Chrome") >-1) {
return "Chrome";
}
if (Useragent.indexof ("Safari") >-1) {
return "Safari";
}//Determine if Safari browser
if (Useragent.indexof ("compatible") >-1 && useragent.indexof ("MSIE") >-1 &&!isopera) {
return "IE";
}; Determine if IE browser
}

Here is the function called above
var mb = Mybrowser ();
if ("IE" = = MB) {
Alert ("I am IE");
}
if ("FF" = = MB) {
Alert ("I am Firefox");
}
if ("Chrome" = = MB) {
Alert ("I am Chrome");
}
if ("Opera" = = MB) {
Alert ("I am Opera");
}
if ("Safari" = = MB) {
Alert ("I am Safari");
}

Second, differentiate the browser, and consider IE5.5 6 7 8

Copy CodeThe code is as follows:
function Mybrowser () {
var useragent = navigator.useragent; Get the useragent string for the browser
var Isopera = Useragent.indexof ("Opera") >-1; Determine if Opera browser
var Isie = Useragent.indexof ("compatible") >-1 && useragent.indexof ("MSIE") >-1 &&!isopera; Determine if IE browser
var IsFF = Useragent.indexof ("Firefox") >-1; Determine if Firefox browser
var Issafari = Useragent.indexof ("Safari") >-1; Determine if Safari browser
if (Isie) {
var IE5 = IE55 = IE6 = IE7 = IE8 = false;
var reie = new RegExp ("MSIE (\\d+\\.\\d+);");
Reie.test (useragent);
var fieversion = parsefloat (regexp["$"]);
IE55 = Fieversion = = 5.5;
IE6 = Fieversion = = 6.0;
IE7 = Fieversion = = 7.0;
IE8 = 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
Here is the function called above
if (mybrowser () = = "FF") {
Alert ("I am Firefox");
}
if (mybrowser () = = "Opera") {
Alert ("I am Opera");
}
if (mybrowser () = = "Safari") {
Alert ("I am Safari");
}
if (mybrowser () = = "IE55") {
Alert ("I am IE5.5");
}
if (mybrowser () = = "IE6") {
Alert ("I am IE6");
}
if (mybrowser () = = "IE7") {
Alert ("I am IE7");
}
if (mybrowser () = = "IE8") {
Alert ("I am IE8");
}

Here is a decision to determine the current browser is IE JS code.

The principle is to make use of the differences between IE and the standard browser in handling the ToString method of the array. For a standard browser, if the last character in the array identifier a comma, the JS engine automatically rejects it.

JS code to determine browser type IE, FF, Opera, Safari, Chrome and version

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.