JavaScript determines IE version number

Source: Internet
Author: User


Today, a project needs to determine the IE version number, and because JQuery 2.0 to remove the browser version number of the judge (it recommended feature detection), so I saw a foreigner wrote a piece of code:

The code is as follows Copy Code

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

This piece of code is really clever! Both brief and backward compatible! The general practice is: 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


The above code is strongly recommended!

The one you wrote

  code is as follows copy code


<span id=" Css "></span>
<script>
if ( Navigator.userAgent.indexOf ("MSIE") >0)
{
//is an IE browser
if (navigator.userAgent.indexOf ("MSIE 6.0") >0
{
//6.0 uses 1. CSS
css.innerhtml= ' <link href= ' 1.css ' rel= ' stylesheet ' type= ' text/css ' > '
}
if ( Navigator.userAgent.indexOf ("MSIE 7.0") >0)
{
//7.0 uses 2. CSS
css.innerhtml= ' <link href= "2.css" rel= "stylesheet" type= "text/css" > '
}
}else
{
//otherwise use 3 . CSS and a specific browser you can use Navigator.useragent to obtain information specific use to see yourself
css.innerhtml= ' <link href= 3.css "rel=" stylesheet "type=" Text/css ">"
}
</script>

The following recommendation of a few words to determine IE browser code

The code is as follows Copy Code

<script type= ' Text/javascript ' >
Notie =-[1,];
if (-[1,]) {
Standard browser Code
}else{
The Code of IE only
}
</script>

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.