jquery to judge the browser by $.browser. _jquery

Source: Internet
Author: User
How to use:
$.browser. [' Browser keyword ']
Copy Code code 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 look at the source of jquery:
Copy Code code 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 the regular to match useragent to determine the type and version of the browser.
What if we're going to judge whether the current browser is IE6?
$.browser.msie&& ($.browser.version = = "6.0") &&!$.support.style
Similarly jquery determines whether the browser is IE7
$.browser.msie&& ($.browser.version = = "7.0")
If backward compatibility is not considered, and you do not want to import jquery in order to determine the browser types
The easiest way to judge IE is
Copy Code code as follows:

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

$.browser is a regular expression to match the useragent to determine the browser version and kind of the. jquery1.3.2 version of the document has been declared Jquery.browser and jquery.browser.version recommended deprecated, you can use the Jquery.suppo RT to replace
But the current situation, jquery.support is not good, and is very difficult to use, we still honestly use $.browser to determine the browser type bar
If it is to determine the version of IE I still recommend the use of IE conditional expression to write JS
Copy Code code as follows:

<!--[If ie]>
<script type= "Text/javascript" >
Alert ("IE")
</script>
<! [endif]-->
<!--[if IE 6]>
<script type= "Text/javascript" >
Alert ("IE6")
</script>
<! [endif]-->
<!--[if IE 7]>
<script type= "Text/javascript" >
Alert ("IE7")
</script>
<! [endif]-->

This is more accurate than the way we manually judge IE versions by $.browser, and we don't have to remember the use of jquery browser.

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.