Browser compatibility is the first problem to be solved by the browser development framework in front-end development. To solve the compatibility problem, you must first determine the browser type and version accurately, the browser version can only be known by analyzing the browser's userAgent. today, we will summarize various judgment methods and hope to help you obtain browser information through JS.
Browser code name: navigator. appCodeName browser name: navigator. appName browser version: navigator. appVersion supports Java: navigator. javaEnabled () MIME type (array): navigator. mimeTypes System Platform: navigator. platform Plug-in (array): navigator. plugins User Agent: navigator. userAgent
Js:
Method 1:
If (window. addEventListener) {alert ("not ie");} else if (window. attachEvent) {alert ("is ie");} else {alert ("this happens when browsers of earlier versions that do not support DHTML are generally supported now )")}
Note: The not IE result is displayed in ie 9 or later.
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:
The difference between IE and the standard browser in handling the toString method of the array is used. For the standard browser, if the last character in the array is a comma, The JS engine will automatically remove it.
If (! + [1,]) alert ("this is Internet Explorer"); else alert ("this is not Internet Explorer ");
Note: "This is not an IE browser" will pop up in IE9 and later versions"
Methods to determine common browsers:
var explorer =navigator.userAgent ;//ie if (explorer.indexOf("MSIE") >= 0) {alert("ie");}//firefox else if (explorer.indexOf("Firefox") >= 0) {alert("Firefox");}//Chromeelse if(explorer.indexOf("Chrome") >= 0){alert("Chrome");}//Operaelse if(explorer.indexOf("Opera") >= 0){alert("Opera");}//Safarielse if(explorer.indexOf("Safari") >= 0){alert("Safari");} //Netscapeelse if(explorer.indexOf("Netscape")>= 0) { alert('Netscape'); }
Navigator. userAgent is used here. IndexOf ("MSIE")> = 0 to determine, of course, you can also use
Navigator. userAgent. indexOf ("MSIE ")! =-1.
How does jquery determine the browser type and 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: The $. browser and $. browser. version are removed from Query 1.9 and replaced
$. Support Method