Summary of js and jquery methods for judging browsers

  • 2020-03-30 03:50:46
  • OfStack

JS gets the browser information


 Browser code name: navigator.appCodeName
 Browser name: navigator.appName
 Browser version no. : navigator.appVersion
 right Java Support: navigator.javaEnabled()
MIME Type (array) : navigator.mimeTypes
 System platform: navigator.platform
 Plugins (array) : navigator.plugins
 User agent: navigator.userAgent

Js four ways to judge Internet explorer:

Method one:


if(window.addEventListener){ 
alert("not ie"); 
}else if(window.attachEvent){ 
alert("is ie"); 
}else{ 
alert(" This happens in unsupported DHTML Older versions of browsers (now generally supported) ") 
}

Note: this method will pop up not IE results in IE9 and above versions of IE

Method 2:


if(document.all){ 
alert("IE"); 
}else{ 
alert("not ie"); 
}

Method 3:


var navigatorName = "Microsoft Internet Explorer"; 
if( navigator.appName == navigatorName ){ 
alert("ie") 
}else{
alert("not ie") 
}

Method 4:
Takes advantage of the difference between the way Internet explorer handles arrays and the standard browser's toString method. For the standard browser, if the last character in an array is a comma, the JS engine will automatically remove it.


if(!+[1,])alert(" This is a ie The browser "); 
else alert(" This is not ie The browser ");


Note: IE9 and above will pop up "this is not IE"

How to judge common browsers:


var explorer =navigator.userAgent ;
//ie 
if (explorer.indexOf("MSIE") >= 0) {
alert("ie");
}
//firefox 
else if (explorer.indexOf("Firefox") >= 0) {
alert("Firefox");
}
//Chrome
else if(explorer.indexOf("Chrome") >= 0){
alert("Chrome");
}
//Opera
else if(explorer.indexOf("Opera") >= 0){
alert("Opera");
}
//Safari
else if(explorer.indexOf("Safari") >= 0){
alert("Safari");
} 
//Netscape
else if(explorer.indexOf("Netscape")>= 0) { 
alert('Netscape'); 
} 

Here we use navigator.useragent. IndexOf (" MSIE ") > Theta is equal to zero, and you can use that, of course
The navigator. UserAgent. IndexOf (" MSIE ")! = -1 for judgment.

How jquery determines the browser type and browser version number


$(document).ready(function(){

  var brow=$.browser;

  var bInfo="";

  if(brow.msie){bInfo="MicrosoftInternetExplorer"+brow.version;}

  if(brow.mozilla){bInfo="MozillaFirefox"+brow.version;}

  if(brow.safari){bInfo="AppleSafari"+brow.version;}

  if(brow.opera){bInfo="Opera"+brow.version;}

  alert(bInfo);

});


Note :Query, starting with version 1.9, removes $. Browser and $. Browser. version and replaces them with

$. Support method


Related articles: