if条件:如果浏览器是IE,IE浏览器版本是否早于9

时间:2010-11-19 15:09:06

标签: jquery internet-explorer internet-explorer-7 if-statement

下面的if条件我认为它说 - 如果浏览器是IE和IE浏览器版本比9更新,但我没有IE 9来测试它所以很难知道正确的输出,这也是不是100%我想要的bcos这个脚本默认也应该在其他浏览器上运行,如Chrome,Firefox等 - 是否可以在if条件下设置它?

if ($.browser.msie && parseInt($.browser.version) > 9) 
{
  // run this code
}

我想使用if条件的原因是脚本似乎在IE 7上有错误,当然最好的办法是修复脚本但是我不知道脚本的哪个部分IE不接受它(所有其他浏览器工作得非常好!)。你知道我可以用来调试IE 6,7,8等脚本的工具吗?我使用notepad ++来编写我的jquery等,所以它不提供任何调试内容......

所以,我的下一个最佳解决方案是,如果它是早于9的IE浏览器,则不运行此脚本。

顺便说一句,这是IE7浏览器上显示的错误信息,但我永远无法理解!

Line:910  //which line?
Char:4   // what the hell is this?
Error: Object doesn't support this property or method //what?
Code: 0    // 0 of what?
URL: http://localhost/mysite/page-1 // so which file is causing the error then? the .js or .html or something else??

血腥的IE!

感谢。

9 个答案:

答案 0 :(得分:12)

您的代码看起来很好,但您忘记在parseInt中设置基数参数:

if ($.browser.msie && parseInt($.browser.version, 10) > 9){

  // you need to wait a couple years to test if it works...
  alert("I'm IE10 or 11...");

}

这不会导致任何错误

答案 1 :(得分:10)

首先,您可以通过从Microsoft网站下载来获取IE9预览:http://ie.microsoft.com/testdrive/

其次,parseInt($.browser.version) > 9可能会检查版本是否大于 9,当然在v10发布之前不会。 (您可能打算>=('大于或等于')?

我经常告诉人们避免浏览器检测或浏览器特定的代码。有时候有必要,但它们很少见。大多数情况下,开发人员可以通过了解失败并解决问题来获得更好的服务(像Modernizr这样的工具对这类事情有帮助。)

然而,有时候只需要进行浏览器检测。

在您的情况下,如果您确实需要检测IE,请不要按照您的方式执行(即检查版本9);检查旧版本会更好,我建议有条件的评论是最好的方法。

<script>
var i_am_old_ie = false;
<!--[if LT IE  9]>
i_am_old_ie = true;
<![endif]-->
</script>

然后您的if()声明可能如下所示:

if(i_am_old_ie) {
   //do stuff for IE6/7/8
} else {
   //do stuff for all other browsers (including IE9)
}

答案 2 :(得分:8)

以下是测试浏览器是否为IE的Javascript版本:

<script>

    function getIEVersion() {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.test(ua) != null)
                rv = parseFloat( RegExp.$1 );
        }
        return rv;
    }


    function checkVersion() {
        var ver = getIEVersion();

        if ( ver != -1 ) {
            if (ver <= 9.0) {
                // do something
            }
        }
    }

    checkVersion();

</script>

如果您正在使用HTML5特定模块并希望在浏览器不支持这些功能的情况下实现某些回退功能,则此方法很有用,例如。 <canvas>

答案 3 :(得分:7)

有一种更简单的方法。

由于这是一条评论,除了IE之外的所有其他浏览器都会被忽略。

var IE;
//@cc_on IE = navigator.appVersion;

然后在你的脚本中使用它来检测所有的IE版本。

if (IE) {
//This is IE.
}
else {
//This is NOT IE.
}

或者匹配任何这样的版本:

if (IE <= 6) {}, if (IE < 9) }, if (IE == 7) {} etc etc...

以下是我找到这些类型条件的来源: http://www.javascriptkit.com/javatutors/conditionalcompile.shtml

答案 4 :(得分:2)

您可以只为目标版本添加一个空div,而不是放入一堆内联javascript,所以将以下内容放在html的顶部,就在body标记内:

<!--[if LT IE  9]>
    <div id="iedetect" class="oldie"></div>
<![endif]-->

然后你可以在你的jquery中放置一个if语句来检查那个块。

if ($("#iedetect").is(".oldie")) {
    //Do something
} else {
   //Do Something else in all other browsers
}

答案 5 :(得分:1)

<!--[if (IE 9)|!(IE)]><!--> Script here <!--<![endif]-->

答案 6 :(得分:1)

我发现级联对于多浏览器检测非常有用。 此代码用于将淡入淡出更改为显示/隐藏,即8 7 6。

$(document).ready(function(){
    if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 8.0)
         { 
             $(".glow").hide();
            $('#shop').hover(function() {
        $(".glow").show();
    }, function() {
        $(".glow").hide();
    });
         }
         else
         { if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 7.0)
         { 
             $(".glow").hide();
            $('#shop').hover(function() {
        $(".glow").show();
    }, function() {
        $(".glow").hide();
    });
         }
         else
         {if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 6.0)
         { 
             $(".glow").hide();
            $('#shop').hover(function() {
        $(".glow").show();
    }, function() {
        $(".glow").hide();
    });
         }
         else
         { $('#shop').hover(function() {
        $(".glow").stop(true).fadeTo("400ms", 1);
    }, function() {
        $(".glow").stop(true).fadeTo("400ms", 0.2);});
         }
         }
         }
       });

答案 7 :(得分:1)

<!--[if IE 8]>
  <script type="text/javascript">
    // this is only executed for IE 8
  </script>
<![endif]-->

答案 8 :(得分:1)

或者你可以这样做

<!--[if IE 9]>
  <script type="text/javascript">
    // this is only executed for IE 9
  </script>
<![endif]-->
相关问题