【问题标题】:If Internet Explorer - Add Script如果 Internet Explorer - 添加脚本
【发布时间】:2012-05-12 11:08:53
【问题描述】:

如果用户使用 Internet Explorer,我正在尝试动态添加脚本。我尝试了 IE 条件,但 chrome 不喜欢 !IE 标记,所以我正在尝试 jquery 方式:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" ></script>
<script type="text/javascript">
    if($.browser.msie){
        document.write('<script type="text/javascript" src="ie/ieFix.js" ></script>');
    }
    else{
        document.write('<script type="text/javascript" src="dynamic.js" ></script>');
    }
</script>

我不知道为什么它不让我添加这个,而是返回'); } else{ document.write(''); }

将它们放入变量中具有相同的效果:/

IE 条件

<!--[if !ie]>-->
    <script type="text/javascript" src="dynamic.js" ></script>
<!--<![endif]-->
<!--[if ie]>-->
    <script type="text/javascript" src="ie/ieFix.js" ></script>
<!--<![endif]-->

【问题讨论】:

    标签: javascript jquery browser cross-browser


    【解决方案1】:

    该行中的&lt;/script&gt;

    document.write('<script type="text/javascript" src="ie/ieFix.js" ></script>');
    

    实际上匹配为结束标记

    <script type="text/javascript">
    

    在它上面的那一行。您应该将其重写为类似

    document.write('<script type="text/javascript" src="ie/ieFix.js" ></scr' + 'ipt>');
    

    这样您就不会将浏览器与结束 &lt;/script&gt; 标记混淆。

    【讨论】:

      【解决方案2】:

      任何 IE 检查

      function detectIEEdge() {
          var ua = window.navigator.userAgent;
      
          var msie = ua.indexOf('MSIE ');
          if (msie > 0) {
              // IE 10 or older => return version number
              return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
          }
      
          var trident = ua.indexOf('Trident/');
          if (trident > 0) {
              // IE 11 => return version number
              var rv = ua.indexOf('rv:');
              return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
          }
      
          var edge = ua.indexOf('Edge/');
          if (edge > 0) {
             // Edge => return version number
             return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
          }
      
          // other browser
          return false;
      }
      

      【讨论】:

      • 在 IE11 中测试。它返回错误??
      • 刚刚更新。
      【解决方案3】:

      如果你做得对,Chrome 可以很好地与 !IE 配合使用:

      <!--[if !ie]>-->
          Put stuff for non-IE browsers here
      <!--<![endif]-->
      

      您在上面的代码中遇到问题的原因是 HTML 解析器将 &lt;/script&gt; 视为脚本的结尾而不是字符串的一部分。人们通常使用&lt;\/script&gt; 来解决这个问题。


      更新:你现在做错了“如果 IE”。

      <!--[if ie]>
          IE stuff - this is a "conditional reveal"
      <![endif]-->
      <!--[if !ie]>-->
          Non-IE stuff - this is a "conditional hide"
      <!--<![endif]-->
      

      【讨论】:

      • 另外,即使我添加了if ie 条件,它也默认为chrome 中的ie 版本。我用我的 ie 条件句编辑了我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      相关资源
      最近更新 更多