Use $. browser under jQuery to determine the browser. _ jquery-js tutorial

Source: Internet
Author: User
JQuery is used to determine the browser type. The usage of the tool class $. browser is mainly used:
$. Browser. ['browser keyword ']

The Code is as follows:


$ (Function (){
If ($. browser. msie ){
Alert ("this is msie ");
}
Else if ($. browser. safari)
{
Alert ("this is safari! ");
}
Else if ($. browser. mozilla)
{
Alert ("this is mozilla! ");
}
Else if ($. browser. opera ){
Alert ("this is opera ");
}
Else {
Alert ("I don't konw! ");
}


Let's take a look at the jQuery source code:

The Code is as follows:


Var userAgent = navigator. userAgent. toLowerCase ();
// Figure out what browser is being used
JQuery. browser = {
Version: (userAgent. match (/. + (? : Rv | it | ra | ie) [\/:] ([\ d.] +)/) | []) [1],
Safari:/webkit/. test (userAgent ),
Opera:/opera/. test (userAgent ),
Msie:/msie/. test (userAgent )&&! /Opera/. test (userAgent ),
Mozilla:/mozilla/. test (userAgent )&&! /(Compatible | webkit)/. test (userAgent)
};


JQuery uses regular expressions to match userAgent to determine the browser type and version.
How can we determine whether the current browser is IE6?
$. Browser. msie & ($. browser. version = "6.0 ")&&! $. Support. style
Similarly, jQuery checks whether the browser is IE7.
$. Browser. msie & ($. browser. version = "7.0 ")
If backward compatibility is not considered, you do not want to import jQuery to determine the browser type.
The simplest way to judge IE is

The Code is as follows:


If (document. all ){
Alert ("IE6 ")
}


$. Browser uses regular expressions to match userAgent to determine the browser version and type. jquery has been declared in the jquery1.3.2 document. browser and jquery. browser. we recommend that you discard the version. You can use jquery. to replace
But in the current situation, jquery. support is not easy to use and is very difficult to use. We still honestly use $. browser to judge the browser type.
To determine the version of IE, we recommend that you use the conditional expression of IE to write JavaScript code.

The Code is as follows:






This is more accurate than we manually use $. browser to determine the IE version, and we do not need to record the use of jquery's browser.
Related Article

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.