Js/jquery Method for Determining browsers _ jquery

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.