jquery $.browser to determine the browser instance

Source: Internet
Author: User

Let's look at the source of jquery:

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 above browser is not enough to judge, we can write by ourselves Oh, the method is also very simple to add your browser name to the jquery function.

If you want to judge IE6 we can do the following

The code is as follows Copy Code

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


Other if ie6,7,8,9,10 can operate this way I will not be introduced 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.