js and jquery Method summary for determining browser type

  • 2020-06-12 08:28:23
  • OfStack

JS gets browser information


Browser code name: navigator.appCodeName
Browser name: navigator.appName
Browser Version: navigator.appVersion
right Java Support: navigator.javaEnabled()
MIME Type (array) : navigator.mimeTypes
System platform: navigator.platform
Plug-in (array) : navigator.plugins
User Agent: navigator.userAgent

js four ways to judge IE browser:

Method 1:


if(window.addEventListener){ 
alert("not ie"); 
}else if(window.attachEvent){ 
alert("is ie"); 
}else{ 
alert(" This situation occurs in unsupported DHTML The old version of the browser (now 1 General support) ") 
}

Note: this method will pop up the not ie result in IE9 and above IE version

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:
It takes advantage of the difference between IE and the standard browser's toString method of handling arrays. For standard browsers, the JS engine automatically removes the last character in the array if it is a comma.


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 browser"

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'); 
} 

navigator. userAgent. indexOf("MSIE") > Theta = 0, of course you can use theta
navigator. userAgent. indexOf (" MSIE ")! Is equal to minus 1.

jquery method for determining 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: Starting with version 1.9, Query has removed.browser and.browser.version, replacing them with the $.support method

This is the end of this article, I hope you enjoy it.


Related articles: