Home > Web Front-end > JS Tutorial > body text

Summary of js/jquery methods to determine browser type_jquery

WBOY
Release: 2016-05-16 15:59:40
Original
1110 people have browsed it

JS to get browser information

Copy code The code is as follows:

Browser code name: navigator.appCodeName
Browser name: navigator.appName
Browser version number: navigator.appVersion
Support for Java: navigator.javaEnabled()
MIME types (array): navigator.mimeTypes
System platform: navigator.platform
Plugins (array): navigator.plugins
User agent: 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的老版本浏览器(现在一般都支持)") 
}
Copy after login

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"); 
}
Copy after login

Method 3:

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

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浏览器");
Copy after login

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

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

});

Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template