Js code determines the type of browser IE FF Opera Safari chrome and version

  • 2020-03-30 03:06:15
  • OfStack

The first is to differentiate between browsers, not versions


function myBrowser(){
    var userAgent = navigator.userAgent; //Gets the browser's userAgent string
    var isOpera = userAgent.indexOf("Opera") > -1;
    if (isOpera) {
        return "Opera"
    }; //Determine if the Opera browser
    if (userAgent.indexOf("Firefox") > -1) {
        return "FF";
    } //Determine if the Firefox browser is
    if (userAgent.indexOf("Chrome") > -1){
  return "Chrome";
 }
    if (userAgent.indexOf("Safari") > -1) {
        return "Safari";
    } //Determine whether Safari is
    if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
        return "IE";
    }; //Determine if Internet explorer
} //Below is a call to the above function
var mb = myBrowser();
if ("IE" == mb) {
    alert(" I am a IE");
}
if ("FF" == mb) {
    alert(" I am a Firefox");
}
if ("Chrome" == mb) {
    alert(" I am a Chrome");
}
if ("Opera" == mb) {
    alert(" I am a Opera");
}
if ("Safari" == mb) {
    alert(" I am a Safari");
}

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


function myBrowser(){
    var userAgent = navigator.userAgent; //Gets the browser's userAgent string
    var isOpera = userAgent.indexOf("Opera") > -1; //Determine if the Opera browser
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //Determine if Internet explorer
    var isFF = userAgent.indexOf("Firefox") > -1; //Determine if the Firefox browser is
    var isSafari = userAgent.indexOf("Safari") > -1; //Determine whether Safari is
    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;
        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
//Below is a call to the above function
if (myBrowser() == "FF") {
    alert(" I am a Firefox");
}
if (myBrowser() == "Opera") {
    alert(" I am a Opera");
}
if (myBrowser() == "Safari") {
    alert(" I am a Safari");
}
if (myBrowser() == "IE55") {
    alert(" I am a IE5.5");
}
if (myBrowser() == "IE6") {
    alert(" I am a IE6");
}
if (myBrowser() == "IE7") {
    alert(" I am a IE7");
}
if (myBrowser() == "IE8") {
    alert(" I am a IE8");
}

Below is a JS code that determines that the current browser is IE.

The principle is to take advantage of the differences between Internet explorer and standard browsers in the toString method of dealing with arrays. For standard browsers, if the last character in the array is a comma, the JS engine will automatically remove it.  





[Ctrl+A]


Related articles: