js code to determine browser types IE, FF, Opera, Safari, chrome and version

Keywords: IE Firefox

Because the version of ie10-ie11 no longer supports document.all judgment, the IE judgment function has to be rewritten.

function isIE() { //ie?
if(!!window.ActiveXObject || "ActiveXObject"in window)
returntrue;
else
returnfalse;
}


The first is to distinguish browsers only, not versions.

function myBrowser(){
    var userAgent = navigator.userAgent; //Get the browser's userAgent string

    var isOpera = userAgent.indexOf("Opera") > -1;

    if (isOpera) {

        return "Opera"

    }; //Judging Opera Browser

    if (userAgent.indexOf("Firefox") > -1) {

        return "FF";

    } //Determine whether Firefox browsers are available

    if (userAgent.indexOf("Chrome") > -1){

  return "Chrome";

 }

    if (userAgent.indexOf("Safari") > -1) {

        return "Safari";

    } //Determine whether Safari browser is available

    if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {

        return "IE";

    }; //Judging whether IE browser or not

}
//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("I am Chrome");

}

if ("Opera" == mb) {

    alert("I am Opera");

}

if ("Safari" == mb) {

    alert("I am Safari");
}

Second, differentiate browsers and consider IE5.5,667 8
function myBrowser(){

    var userAgent = navigator.userAgent; //Get the browser's userAgent string

    var isOpera = userAgent.indexOf("Opera") > -1; //Judging Opera Browser

    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //Judging whether IE browser or not

    var isFF = userAgent.indexOf("Firefox") > -1; //Determine whether Firefox browsers are available

    var isSafari = userAgent.indexOf("Safari") > -1; //Determine whether Safari browser is available

    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

//The following is a call to the above function

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 JS code to determine that the current browser is IE.

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

<script>

var ie = !-[1,]; 

alert(ie); 

</script>




Posted by twoeyes on Tue, 12 Feb 2019 16:03:18 -0800