Browser judgment and IE version Differentiation

Source: Internet
Author: User

  Note: JS execution is inconsistent under Firefox and IE, and the results of many statements are inconsistent. It is also possible for other browsers. Verify that if else includes window. Onload;

①Only used to differentiate IE and non-ie kernelsBecause only ie supports ActiveX controls, you can useWindow. activexobjectTo determine, anotherDocument. AllIt can also be used to determine. If the returned value is true, it indicates that the IE kernel (If only the item is used to identify whether IE is used, use this method.);

VaR isie = Window. activexobject;
If (isie ){
Alert ('ie kernels ');
}
Else {
Alert ('non-ie kernels ');
}

②Determine whether the IE kernel and browser version are used,Use navigator. useragent to determine

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.

<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 the future, if (sys. IE) to identify whether or not the IE kernel, use if (sys. ie = '6. 0 '), if (sys. firefox = '12. 0 ') to differentiate the browser version;

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.