Home  >  Article  >  Web Front-end  >  Summary of js/jquery methods to determine browser_jquery

Summary of js/jquery methods to determine browser_jquery

WBOY
WBOYOriginal
2016-05-16 16:38:031280browse

JS gets browser information

浏览器代码名称:navigator.appCodeName
浏览器名称:navigator.appName
浏览器版本号:navigator.appVersion
对Java的支持:navigator.javaEnabled()
MIME类型(数组):navigator.mimeTypes
系统平台:navigator.platform
插件(数组):navigator.plugins
用户代理:navigator.userAgent

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 three:

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 versions will pop up "This is not IE browser"

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 is used here. indexOf("MSIE") >= 0 for judgment, of course you can also use
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 has removed $.browser and $.browser.version starting from version 1.9 and replaced them with

$.support method

Statement:
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