仅在IE7中禁用javascript

时间:2013-07-16 17:15:18

标签: javascript jquery browser internet-explorer-7

我有一个场景,我需要在除IE7之外的所有浏览器中加载JavaScript。任何帮助是极大的赞赏。我尝试过以下代码:

<!--[if gt IE 7]>
<script type="text/javascript">
    //Some script XXX to execute
</script>
<![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<script type="text/javascript">    
    //Some script XXX to execute
</script>
<!--<![endif]-->

4 个答案:

答案 0 :(得分:1)

我认为这也有效

<!--[if !(IE 7)]>
   // tag here
<![endif]-->

您也可以组合使用

<![if !IE]>
   // script tag
<![endif]>

<!--[if (gte IE 8)&(lt IE 7)]>
   // script tag
<![endif]-->

请参阅此处的文档http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx

我不确定您是要加载整个文件还是只需几行脚本。如果只有几行,则jquery方式更容易

答案 1 :(得分:0)

使用navigator.userAgent检查浏览器标识。 also see here

答案 2 :(得分:0)

试试这个

if ( $.browser.msie == true &&  $.browser.version < 8) {
           //code for ie 7
        }
else
{
       document.write("<script src=\"test.js\">");
}

答案 3 :(得分:0)

看起来您已经使用conditional comments

排在正确的位置
<!--[if !(IE 7)]>--><script>alert("I'm not IE7");</script><!--<![endif]-->

对于任何非IE浏览器,这都变为

<!--[if !(IE 7)]>-->                     Comment
<script>alert("I'm not IE7");</script>   Element
<!--<![endif]-->                         Comment

对于任何IE浏览器,这都变为

<!--[if !(IE 7)]>                        If not IE 7
-->                                      (continued) AND Comment
<script>alert("I'm not IE7");</script>   Element
<!--<![endif]-->                         Comment AND End if

从中,如果它是IE 7,它就会被剪断。

<!--[if !(IE 7)]>                        If - not rendered
                                         SNIP
<![endif]-->                             End If

如果您不介意无效的HTML

,可以减少这种情况
<![if !(IE 7)]><script>alert("I'm not IE7");</script><![endif]>