[收藏]判断IE的版本

分类:Javascript| 发布:camnprbubuol| 查看:1813 | 发表时间:2013/7/12

在判断当前浏览器是否IE的方法很多,比如下边放到if条件里边都可以说明是ie浏览器,
!-[1,] window.VBArray window.ActiveXObject window.attachEvent 等等,基本都是利用ie的奇葩(因为其它主流浏览器都很正统:-))

应用例子JS代码如下:

1if (-[1, ]) {//非IE
2  e.preventDefault();
3} else {//IE
4   e.cancelBubble = true;
5}

上边是判断ie和其它浏览器的鉴别,那么ie本身又分ie5,ie6,ie7,ie8,ie9,ie10,ie11... ie5我们可以忽略了,ie6也快应该被忽略了,期待!!!

ie9像是ie家族的一个分水岭,不过也不是很明显罢了,有几个需要注意一下:
Trident 标识是在 IE9 中引入的。 IE10 才不支持 条件注释

我们都知道,判断ie的版本可以通过user-agent,那么我们第一个方法就用这个吧,也算回顾一下。(重点是第二个方法,请期待一下下吧。(*^__^*) )

方法一: navigator.userAgent

你可以直接在你的浏览器的地址栏中输入:javascript:document.write(navigator.userAgent) 然后回车,看一下,自己的浏览器是一串什么代码。

Internet Explorer 11 的 user-agent:

Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko

Internet Explorer 10 的 user-agent (on Windows 7):

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)

上述 user-agent 你还会注意到增加了 Gecko 的标识,而 Safari 是首个标注了 Gecko 的浏览器。

下边是ie9-ie5的user-agent:

Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E; BOIE9;ZHCN)

 

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E; BOIE9;ZHCN)

 

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E; BOIE9;ZHCN)

 

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E; BOIE9;ZHCN)

 

Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 6.1; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E; BOIE9;ZHCN)

方法二:号称是最短的判断ie版本的方法

JS代码如下:

01// ----------------------------------------------------------
02// 一个简短的js来探测ie的版本
03// 没有用 user-agent 的方法嗅探
04// ----------------------------------------------------------
05// If you're not in IE (or IE version is less than 5) then:
06//     ie === undefined
07// If you're in IE (>=5) then you can determine which version:
08//     ie === 7; // IE7
09// Thus, to detect IE:
10//     if (ie) {}
11// And to detect the version:
12//     ie === 6 // IE6
13//     ie > 7 // IE8, IE9 ...
14//     ie < 9 // Anything less than IE9
15// ----------------------------------------------------------
16// UPDATE: Now using Live NodeList idea from @jdalton
17var ie = (function(){
18    var undef,
19        v = 3,
20        div = document.createElement('div'),
21        all = div.getElementsByTagName('i');
22  
23    while (
24        div.innerHTML ='<!--[if gt IE' + (++v) +']><i></i><![endif]-->',
25        all[0]
26    );
27    return v > 4 ? v : undef;
28}());

注意一下这个 while 语句。是我觉得最有趣的。对于逗号操作符。我也不熟悉,还只是停留在像变量定义的用法上。比如:

1var  a='b', c ='d', e ='f';
2var obj = {
3a:'b',
4c:'d',
5e:'f'
6}

其实这个比较少见。通常是返回最后一个值。

1var a = (1,2,3,5,6,0,9,4); // a === 4

当IE的浏览器模式(或者文档模式)改变的话,我怎么检测当前IE浏览器的版本?

以上两种方法,分别用了User Agent 探测条件注释方法,那么接下来这种方法是:条件编译

如下图,本身浏览器是IE9,可是我改变了(例如使用:X-UA-Compatible改变)浏览器模式,那么userAgent也就随之变化。

IE浏览器模式(或者文档模式)

此时,我们使用方法一: navigator.userAgent获取的就是IE8的信息,因此不能真正的判断出,当前的IE版本。

例如真正的检测浏览器版本是否IE10的方法:

1  var isIE10 = false;
2    /*@cc_on
3        if (/^10/.test(@_jscript_version)) {
4            isIE10 = true;
5        }
6    @*/
7    console.log(isIE10);

然后使用下边的代码做判断,是否是IE10

1if (isIE10) {
2    // Using Internet Explorer 10
3  }

真正检测IE浏览器版本(v5.5~v11)的方法:【收藏了】

01var IE = (function () {
02    "use strict";
03  
04    var ret, isTheBrowser,
05        actualVersion,
06        jscriptMap, jscriptVersion;
07  
08    isTheBrowser = false;
09    jscriptMap = {
10        "5.5": "5.5",
11        "5.6": "6",
12        "5.7": "7",
13        "5.8": "8",
14        "9": "9",
15        "10": "10",
16        "11": "11"
17    };
18    jscriptVersion = new Function("/*@cc_on return @_jscript_version; @*/")();
19  
20    if (jscriptVersion !== undefined) {
21        isTheBrowser = true;
22        actualVersion = jscriptMap[jscriptVersion];
23    }
24  
25    ret = {
26        isTheBrowser: isTheBrowser,
27        actualVersion: actualVersion
28    };
29  
30    return ret;
31}());

以上函数,使用方法是:用IE.isTheBrowser来判断当前浏览器是否是IE浏览器,返回类型布尔值,true表示IE,false表示非IE。 用IE.actualVersion来判断IE浏览器的版本号,返回类型字符串,错误的话,返回undefined

扩展阅读:

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/archives/judge-ie-version.html