Use jquery to detect the script code of browsers and versions

Source: Internet
Author: User

How to use JavaScript to detect browsers is a common issue. Based on my experience, using JavaScript to detect browsers is nothing more than two types of methods.

The first is to use the functional properties of the browser. For example, you can check whether the browser supports the getelementbyid method.

If (document. getelementbyid ){
// The method exists, so use it here
} Else {
// Do something else
}

Although such detection does not know which browser the user uses, developers can determine whether the browser is compatible with their own functions.CodeIs able to withstand the test. This method can be used if you focus on the actual functions of the browser and do not care about its actual identity.

The second is to use the traditional User-Agent string, which may be the oldest and most popular detection method. Although technically speaking, you can change your User-Agent, you can use it to obtain some useful information.

This may be a problem. Anyone who has used jquery knows that using jquery's Brower method can accurately determine the browser or even version users are using. If users of a good development library want to understand some of its implementation mechanisms, how does jquery achieve this?

View jquery's latestSource code(Version 1.2.2) is a function for judging the browser from rows 1,195th to rows 1205. As you can see, jquery uses the second method above, that is, using User-Agent to determine the user's browser and version.

Frankly speaking, at first I was surprised to know the types and versions of browsers with just five lines of code. In JavaScript advancedProgramIn design, the author even uses separate chapters to describe how to use JavaScript to determine the browser and operating system. However, by reading its Code (which is not difficult in fact), I suddenly realized it. Paste the 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)
};

Speaking of this, experienced JavaScript developers already know the secrets. Yes, jquery uses regular expressions to determine the browser type and version. Pretty well done!

First, it unified the User-Agent into lower case, and then gradually matched the browser using regular expressions. For more information about regular expressions, see here. However, some may doubt whether such a judgment is correct. Let's take a look at the User-Agent of the following four mainstream browsers:

Safari (Windows Edition)

... Applewebkit/523.12.9 (khtml, like gecko) version/3.0 Safari/523.12.9

Opera (opera 9.2 on Windows XP)

Opera/9.24 (Windows NT 5.1; U; ZH-CN)

Mozilla (Firefox 2.0.11 on Windows XP)

... Windows NT 5.1; ZH-CN; RV: 1.8.1.11) Gecko/20071127 Firefox/2.0.0.11

Internet Explorer (7.0 on Windows XP)

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

Jquery uses the different User-Agent features of different browsers as its best judgment. For example, in Safari, "/WebKit/" is proprietary and "/Opera/" is only available in the Opera Browser. This verification method can be accurately identified by mainstream browsers.

Here, jquery is indeed one of the excellent Javascript development frameworks. Mastering it can indeed add a lot of fun for your own development. I will post my experiences reading the jquery framework one by one. Please pay attention to it.

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.