Home > Web Front-end > JS Tutorial > js code to determine browser type IE, FF, Opera, Safari, chrome and version_javascript skills

js code to determine browser type IE, FF, Opera, Safari, chrome and version_javascript skills

WBOY
Release: 2016-05-16 16:46:42
Original
1293 people have browsed it

Due to ie10-ie11 version issues, document.all judgment is no longer supported, so the ie judgment function needs to be rewritten

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

The first type only distinguishes the browser, regardless of the version

Copy code The code is as follows:

function myBrowser(){
var userAgent = navigator.userAgent; //Get the browser’s userAgent string
var isOpera = userAgent.indexOf("Opera") > -1;
If (isOpera) {
         return "Opera"
}; //Determine whether Opera browser
If (userAgent.indexOf("Firefox") > -1) {
         return "FF";
} //Determine whether Firefox browser
If (userAgent.indexOf("Chrome") > -1){
Return "Chrome";
}
If (userAgent.indexOf("Safari") > -1) {
         return "Safari";
} //Determine whether Safari browser
If (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
         return "IE";
}; //Determine whether it is IE browser
}

//The following is to call 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 between browsers and consider IE5.5 6 7 8

Copy code The code is as follows:

function myBrowser(){
var userAgent = navigator.userAgent; //Get the browser’s userAgent string
var isOpera = userAgent.indexOf("Opera") > -1; //Determine whether the Opera browser is
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //Determine whether IE browser
var isFF = userAgent.indexOf("Firefox") > -1; //Determine whether Firefox browser
var isSafari = userAgent.indexOf("Safari") > -1; //Determine whether 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["$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 to call 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");
}

The following is a JS code that determines whether the current browser is IE.

The principle is made by taking advantage of the difference in the toString method of processing arrays between IE and standard browsers. For standard browsers, if the last character in the array is a comma, the JS engine will automatically remove it.


[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<script> var ie = !-[1,]; alert(ie); </script>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template