JavaScript determines IE browser version number

Source: Internet
Author: User


Example

The code is as follows Copy Code

MSIE = Int (/msie (d+)/.exec ((navigator.useragent). toLowerCase ()) | | []) [1]);
/* IE 11 changed its useragent format, so to further judge * *
if (isNaN (MSIE)) {
MSIE = int (/trident/.*; rv: (d+)/.exec ((navigator.useragent). toUpperCase ()) | | []) [1]);
}

Example

Good idea, add a DIV element, and then use the while loop to determine whether the div tag using IE conditional annotation contains the I tag, and if it does, V will increase by 1 until the browser cannot read the I tag. The V value at this time if greater than 4 is the actual version number of IE, or V is less than IE5 IE browser or non IE browser, return false.

The code is as follows Copy Code

var ieversion = (function () {
var v = 3,
div = document.createelement (' div '),
all = Div.getelementsbytagname (' i ');
while (
div.innerhtml = ' <!--[if GT IE ' + (++v) + ']><i></i><! [endif]--> ',
ALL[0]
); Www.111cn.net
Return v > 4? V:false;
})();

Example jquery

When you want to use it, you can use $.BROWSER.MSIE6~10 to do the judgment processing. The other is to directly modify the JQuery.browser.version of the method:

The code is as follows Copy Code

<script type= "Text/javascript" src= "/jquery.min.js" ></script>
<script type= "Text/javascript" >
$ (function () {
var useragent = Window.navigator.userAgent.toLowerCase ();

$.browser.msie10 = $.browser.msie &&/msie 10.0/i.test (useragent);
$.BROWSER.MSIE9 = $.browser.msie &&/msie 9.0/i.test (useragent);
$.BROWSER.MSIE8 = $.browser.msie &&/msie 8.0/i.test (useragent);
$.browser.msie7 = $.browser.msie &&/msie 7.0/i.test (useragent);
$.browser.msie6 =!$.browser.msie8 &&!$.browser.msie7 && $.browser.msie &&/msie 6.0/i.test ( useragent);
Www.111cn.net
$ (". Info"). HTML (
"""""");
});
</script>

<body>
<div class= "Info" ></div>
</body>

Final summary

Regular search user_agent;
But because of historical reasons, User_agent has been inaccurate, and by the major manufacturers to change the mess.
Like what:

ie10:mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; trident/6.0)

ie11:mozilla/5.0 (Windows NT 6.3; trident/7.0; RV 11.0) Like Gecko

So we should take the standard as the main.

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.