Home >Web Front-end >JS Tutorial >Summary of js/jquery methods to determine browser type_jquery
JS to get browser information
Four ways to use js to determine IE browser:
Method 1:
if(window.addEventListener){ alert("not ie"); }else if(window.attachEvent){ alert("is ie"); }else{ alert("这种情况发生在不支持DHTML的老版本浏览器(现在一般都支持)") }
Note: This method will pop up not ie results in IE9 and above IE versions
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 in the toString method of array processing 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.
if(!+[1,])alert("这是ie浏览器"); else alert("这不是ie浏览器");
Note: IE9 and above will pop up "This is not IE"
How to determine commonly used 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") >= 0 is used here for judgment. Of course,
can also be used
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: Starting from version 1.9, Query has removed .browser and .browser.version and replaced them with the $.support method
The above is the entire content of this article, I hope you all like it.