Jquery $. browser to determine the browser instance

Source: Internet
Author: User

The browser I used previously was a self-written js method. The principle was that I used js to get the userAgent value and then made the indexOf judgment. Now I used jquery $. browser is not only simple but also highly compatible. I will introduce it to you below.

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

The Code is as follows: Copy code

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

Test usage

$. Browser. ['browser keyword ']

The Code is as follows: Copy code

$ (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! ");
}

If the browser above is not enough to judge, we can write it by ourselves. The method is also very simple. Just add your browser name in the jquery function.

If you want to judge IE6, we can perform the following operations:

The Code is as follows: Copy code

$. Browser. msie & ($. browser. version = "6.0 ")&&! $. Support. style


If ie6, 7, 8, 9, and 10 are all available, I will not introduce them here.

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.