【问题标题】:disable a javascript only in IE7仅在 IE7 中禁用 javascript
【发布时间】:2013-07-15 00:19:30
【问题描述】:

我遇到了一个场景,我需要在除 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]-->

【问题讨论】:

  • 这是什么废话??
  • 如果你使用 jquery 为什么不使用它来检测浏览器版本并进行条件执行。
  • 我标记了重复的,相同的想法,而不是 8,使用 7。

标签: javascript jquery browser internet-explorer-7


【解决方案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 方式会更容易

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      试试这个

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

      【讨论】:

      • 请注意 $.browser 已从 jQuery 1.9 版本中删除。
      • 这个问题没有用 jQuery 标记(虽然它在代码中使用过)
      【解决方案4】:

      看起来你已经在 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]>
      

      【讨论】:

        猜你喜欢
        • 2011-04-01
        • 2023-03-10
        • 2010-09-26
        • 2019-04-23
        • 2010-10-31
        • 1970-01-01
        • 1970-01-01
        • 2011-06-21
        • 1970-01-01
        相关资源
        最近更新 更多