Determine the browser type and version using JavaScript

Source: Internet
Author: User

Do you know how many browsers are available in the world? In addition to the four well-known browsers, ie, Firefox, opera, and Safari, there are nearly browsers in the world.

A few days ago, the browser family gave birth to a little prince, Google's Chrome browser. Because of the birth of chrome, although he is still a little guy, no one dares to underestimate him. In the future, we often say that the browser's "Four talents" has to be renamed as "five golden flowers.

In front-end development of websites, browser compatibility problems have left us in a hurry. Chrome's birth does not know how much trouble we need to add. Browser compatibility is the first problem to be solved by the front-end development framework. To solve the compatibility problem, you must first determine the browser type and version accurately.

Javascript is the main language for front-end development. We can compile javascript programs to determine the browser type and version. There are two methods to determine the browser type in javascript: one is to distinguish based on the unique attributes of various browsers, and the other is to judge by analyzing the useragent attributes of the browser. In many cases, after determining the browser type, you must determine the browser version to handle compatibility issues. However, you can only determine the browser version by analyzing the browser's useragent.

Let's analyze the features of various browsers and Their useragent.

IE

Only ie supports the creation of ActiveX controls, so she has something that is not available in other browsers, namely the activexobject function. As long as you determine that the window object has the activexobject function, you can clearly determine that the current browser is IE. The typical useragent versions of IE are as follows:

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)

The version number is a number after MSIE.

Firefox

All DOM elements in Firefox have a getboxobjectfor function to obtain the location and size of the DOM element (ie corresponds to the getboundingclientrect function ). This is exclusive to Firefox. It can be determined that the current browser is Firefox. The useragent versions of Firefox are roughly as follows:

Mozilla/5.0 (windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1
Mozilla/5.0 (windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3
Mozilla/5.0 (windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12
The version number is a number after Firefox.

Opera

Opera provides a special browser flag, that is, the window. Opera attribute. The typical useragent of opera is as follows:

Opera/9.27 (Windows NT 5.2; U; ZH-CN)
Opera/8.0 (Macintosh; PPC Mac OS X; U; en)
Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) opera 8.0

The version number is a number near opera.

Safari

Safari has an opendatabase function that is not available in other browsers and can be used as a flag for judging safari. The typical useragent of Safari is as follows:

Mozilla/5.0 (windows; U; Windows NT 5.2) applewebkit/525.13 (khtml, like gecko) version/3.1 Safari/525.13
Mozilla/5.0 (iPhone; U; CPU like Mac OS X) applewebkit/420.1 (khtml, like gecko) version/3.0 mobile/4a93 Safari/419.3

The version number is a number after version.

Chrome

Chrome has a messageevent function, but Firefox does. However, Chrome does not have the getboxobjectfor function of Firefox. Based on this condition, the Chrome browser can be accurately determined. Currently, Chrome's useragent is:

Mozilla/5.0 (windows; U; Windows NT 5.2) applewebkit/525.13 (khtml, like gecko) Chrome/0.2.149.27 Safari/525.13

The version number is only the number after chrome.

Interestingly, Chrome's useragent also contains safari features. Maybe this is the basis for chrome to run all apple browser applications.

Once we understand the above information, we can base these features to determine the browser type and version. We will save the judgment results in the SYS namespace and become the basic identifier information of the front-end framework for future programs to read. If a browser is identified, sys namespace will have an attribute of the browser name, whose value is the version number of the browser. For example, if ie 7.0 is determined, SYS. IE is 7.0; If Firefox 3.0 is determined, SYS. Firefox is 3.0. The following code determines the browser:

<SCRIPT type = "text/JavaScript">
VaR sys = {};
VaR UA = navigator. useragent. tolowercase ();
If (window. activexobject)
SYS. Ie = UA. Match (/MSIE ([\ D.] +)/) [1]
Else if (document. getboxobjectfor)
SYS. Firefox = UA. Match (/Firefox \/([\ D.] +)/) [1]
Else if (window. messageevent &&! Document. getboxobjectfor)
SYS. Chrome = UA. Match (/chrome \/([\ D.] +)/) [1]
Else if (window. Opera)
SYS. Opera = UA. Match (/opera. ([\ D.] +)/) [1]
Else if (window. opendatabase)
SYS. Safari = UA. Match (/version \/([\ D.] +)/) [1];

// Perform the following tests:
If (SYS. ie) document. Write ('ie: '+ SYS. ie );
If (SYS. Firefox) document. Write ('Firefox: '+ SYS. Firefox );
If (SYS. Chrome) document. Write ('chrome: '+ SYS. Chrome );
If (SYS. Opera) document. Write ('Opera: '+ SYS. Opera );
If (SYS. Safari) document. Write ('safari: '+ SYS. Safari );

</SCRIPT>

We put the IE judgment first, because IE has the most users, followed by Firefox. Judging the browser type in the order of users can improve the judgment efficiency and reduce useless work. We put chrome in the third judgment because we predict that Chrome will soon become the third-largest browser in the market. When analyzing the browser version, the regular expression is used to extract the version information.

If your JavaScript code is very popular, you can also write the previous judgment Code as follows:

<SCRIPT type = "text/JavaScript">
VaR sys = {};
VaR UA = navigator. useragent. tolowercase ();
Window. activexobject? SYS. Ie = UA. Match (/MSIE ([\ D.] +)/) [1]:
Document. getboxobjectfor? SYS. Firefox = UA. Match (/Firefox \/([\ D.] +)/) [1]:
Window. messageevent &&! Document. getboxobjectfor? SYS. Chrome = UA. Match (/chrome \/([\ D.] +)/) [1]:
Window. Opera? SYS. Opera = UA. Match (/opera. ([\ D.] +)/) [1]:
Window. opendatabase? SYS. Safari = UA. Match (/version \/([\ D.] +)/) [1]: 0;

// Perform the following tests:
If (SYS. ie) document. Write ('ie: '+ SYS. ie );
If (SYS. Firefox) document. Write ('Firefox: '+ SYS. Firefox );
If (SYS. Chrome) document. Write ('chrome: '+ SYS. Chrome );
If (SYS. Opera) document. Write ('Opera: '+ SYS. Opera );
If (SYS. Safari) document. Write ('safari: '+ SYS. Safari );
</SCRIPT>

This simplifies JavaScript code. Of course, the readability is slightly lower, depending on whether you pay attention to efficiency or maintainability.

Different features are used to determine the browser method. Although it is faster than using regular expressions to analyze useragent, these features may change with the browser version. For example, if a browser has been successful in the market due to its unique features, other browsers may also add this feature to it, so that the unique features of the browser disappear, leading to failure in our judgment. Therefore, it is relatively safe to determine the browser type by parsing the features in useragent. Besides, to determine the version information, you also need to parse the browser's useragent.

By analyzing the useragent information of various browsers, it is not difficult to determine the regular expressions of various browsers and their versions. In addition, the judgment on the browser type and version can be fully integrated. Therefore, we can write the following code:

<SCRIPT type = "text/JavaScript">
VaR sys = {};
VaR UA = navigator. useragent. tolowercase ();
VaR S;
(S = UA. Match (/MSIE ([\ D.] + )/))? SYS. Ie = s [1]:
(S = UA. Match (/Firefox \/([\ D.] + )/))? SYS. Firefox = s [1]:
(S = UA. Match (/chrome \/([\ D.] + )/))? SYS. Chrome = s [1]:
(S = UA. Match (/opera. ([\ D.] + )/))? SYS. Opera = s [1]:
(S = UA. Match (/version \/([\ D.] +). * Safari /))? SYS. Safari = s [1]: 0;

// Perform the following tests:
If (SYS. ie) document. Write ('ie: '+ SYS. ie );
If (SYS. Firefox) document. Write ('Firefox: '+ SYS. Firefox );
If (SYS. Chrome) document. Write ('chrome: '+ SYS. Chrome );
If (SYS. Opera) document. Write ('Opera: '+ SYS. Opera );
If (SYS. Safari) document. Write ('safari: '+ SYS. Safari );
</SCRIPT>
In this example, "...?" is used. ...:... "To streamline the code. The judgment condition is a value assignment statement that completes Regular Expression matching and result copying, and directly serves as a condition judgment. The subsequent version information only needs to be extracted from the previous matching results, which is a very efficient code.

The above code is pre-developed to build a front-end framework and tested on five browsers. In the future, to judge a browser, you only need to use if (sys. IE) or if (sys. to determine the browser version, you only need to use if (sys. ie = '8. 0 ') or if (sys. firefox = '3. 0.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/nileel/archive/2009/04/17/4087159.aspx

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.