如何测试Internet Explorer版本是版本7还是版本8?

时间:2012-04-05 12:11:35

标签: javascript jquery

我想测试IE是版本7还是8,是否阻止特定代码运行?

我尝试过以下代码,但这似乎不起作用:

if($.browser.msie && parseInt($.browser.version, 10) <= 8) {
        $(document).on('mouseenter', '.thumb', function () {
          $(this).find('.bgg').stop().animate({ opacity : 1 });
        });

        $(document).on('mouseleave', '.thumb', function () {
          $(this).find('.bg').stop().animate({ opacity : .5 });
        });
      }

理想情况下,我真的不想使用这种检测,但在这种情况下必须使用它。

4 个答案:

答案 0 :(得分:4)

万无一失的方法:

<!--[if lte IE 8]><script type="text/javascript">
    // specific code for IE8 and below goes here.
</script><![endif]-->

答案 1 :(得分:2)

由于要求只有一个脚本文件,我不得不在我的项目中使用UA嗅探IE。我们不希望@ Kolink的方法需要额外的http请求,也不想分割功能。为此我只会使用:

var ltie9 = $.browser.msie && parseInt($.browser.version, 10) < 9;

然后使用:

做任何你想做的事
if (ltie9) { ... }

我有一个jsFiddle,它显示了几个不同的IE检测到IE10只是为了演示。

答案 2 :(得分:0)

使用object detection。 IE7不提供querySelector方法,例如替换

if ($.browser.msie && parseInt($.browser.version, 10) <= 8)

 if (document.all && !document.querySelector)

Here还有更多想法

答案 3 :(得分:0)

IE8可以像IE7一样使用 -

要区分,可以测试document.documentMode

向导航器对象添加属性可以节省必须再次测试

//(Run= {};

if(window.addEventListener){
    Run.handler= function(who, typ, fun){
        if(who && who.addEventListener) who.addEventListener(typ, fun, false);
    }// all browsers except IE8 and below
}
else if(window.attachEvent){
    /*@cc_on
    @if(@_jscript_version>5.5){
        navigator.IEmod= document.documentMode?
        document.documentMode:window.XMLHttpRequest? 7:6;
    }
    @end
    @*/
    Run.handler= function(who, typ, fun){
        if(who && who.attachEvent){
            who.detachEvent('on'+typ, fun);
            who.attachEvent('on'+typ, fun);
        }
    }
}