【问题标题】:How to load a script only in IE如何仅在 IE 中加载脚本
【发布时间】:2015-07-11 08:24:10
【问题描述】:

我只需要在 Internet Explorer 浏览器中触发特定脚本!

我试过了:

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

不幸的是,这实际上阻止了脚本的加载。

编辑:对于问我为什么需要这个的每个人:IE 在使用某些动画时会使滚动变得非常跳跃。为了解决这个问题,我需要实现一个为 IE 提供平滑滚动的脚本。我不想将它应用到其他浏览器,因为它们不需要它,而且这个脚本虽然使滚动更平滑也让它有点不自然。

【问题讨论】:

标签: javascript internet-explorer conditional-statements


【解决方案1】:

currentScript 在所有浏览器中[支持][1]除了 IE

<script>
    // self-invoked wrapper for scoping the `document` variable
    !function( d ) {
        if( !d.currentScript ){
            var s = d.createElement('script')
            s.src = 'ie.js'
            d.head.appendChild(s)
        }
    }(document)
</script>

以上将有条件地为 IE 浏览器附加一个脚本文件。 [1]:https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript#Browser_compatibility

【讨论】:

  • 这是迄今为止最优雅的答案。
  • 比起依赖用户代理解析,我更喜欢这种策略。不过,在 Opera Mini 上,currentScript 检测也会失败。如果您想确保 only IE 获取额外的脚本,我建议检查 !(window.CSS.supports || window.supportsCSS)。 (support table)
  • 为什么在function(d)之前需要!
  • @BruceSun - 它是自调用函数的较短版本 - read more here
【解决方案2】:

如果有人仍在查看运行 IE 特定的 Javascript,那么此代码仍然可以在 IE(11) 和非 IE 浏览器(Chrome、Firefox、Edge)中进行测试

<script type="text/javascript">
    if(/MSIE \d|Trident.*rv:/.test(navigator.userAgent))
        document.write('<script src="../nolng/js/ex1IE.js"><\/script>
        <script src="../nolng/js/ex2IE.js"><\/script>');
    else
        document.write('<script src="../nolng/js/ex1.js"><\/script>
       <script src="../nolng/js/ex2.js"><\/script>');
</script>

【讨论】:

    【解决方案3】:

    您可以使用 javascript 来检测 IE 并动态插入您的脚本:

    var ua = window.navigator.userAgent;
    if (ua.indexOf("MSIE ") != -1|| !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.setAttribute('src', 'YOUR_JS_SCRIPT.js');
        script.setAttribute('type', 'text/javascript');
        script.setAttribute('async', 'false');
        head.appendChild(script);
    }
    

    如果你可以使用 jQuery,你可以使用更短的代码:

    if (ua.indexOf("MSIE ") != -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
        $.getScript('YOUR_JS_SCRIPT.js');
    }
    

    在这篇帖子Check if user is using IE with jQuery找到解决方案

    【讨论】:

    • IE11 在用户代理字符串中没有 MSIE。
    • 您还应该检查indexOf 的结果是否大于-1。如果用户代理不包含MSIE,它将返回-1,这是 if 语句中的真实值。
    【解决方案4】:

    您可以修改此脚本以运行特定于 IE 的 JavaScript:

    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");
    
    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
      alert('IE ' + parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
    else alert('otherbrowser');
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

    【讨论】:

    • 看代码,它不需要JQuery,但我暂时无法测试。
    【解决方案5】:

    我很好奇为什么您特别需要针对 IE 浏览器,但如果您确实需要这样做,以下代码应该可以工作:

    <script type="text/javascript">
        if(/MSIE \d|Trident.*rv:/.test(navigator.userAgent))
            document.write('<script src="somescript.js"><\/script>');
    </script>
    

    Regex 的前半部分 (MSIE \d) 用于检测 Internet Explorer 10 及更低版本。后半部分用于检测 IE11 (Trident.*rv:)。

    如果浏览器的用户代理字符串与该模式匹配,它会将somescript.js 附加到页面。

    【讨论】:

    • 好吧,IE 恰好有一个称为平滑滚动(“工具”>“Internet 选项”>“高级”>“使用平滑​​滚动”)的神奇功能,它默认选中,实际上会产生各种问题而不是解决任何问题。在我的特殊情况下,我有一些滑入动画,只有在 IE 上才会使滚动变得非常跳跃,除非你关闭了平滑滚动。所以我只需要为 IE 添加一个滚动脚本。跨度>
    • 您好,我有一些要求,我需要一些 JS 文件才能在所有浏览器中调用,但在 IE10 和 IE9 中不需要。哪种条件适用?
    • 这看起来不错,但是如果你想立即执行呢?
    • @codepleb 由于document.write,它应该在之后同步运行,但我不确定我今天是否会推荐这个旧答案,因为那里有一些经过更好测试的库和更安全地解决这个问题。
    • 六年后,需要这个的主要原因是因为 IE11 不会消亡,而其他一切早已转移到 ES6,而 IE11 则停留在现已死去的 ES5 时代。让 IE11 拉出特殊的“仅适用于 ie11”未优化的 ES5 版本,而其他所有浏览器正在获得现代、优化的 ES6 js 有效负载实际上变得更加重要,而不是不那么重要 =(
    【解决方案6】:

    功能检测是一种更好的方法 - 我建议您研究 Modernizr,以便在设备/浏览器能够支持时逐步增强您的解决方案。

    这种基于 CSS 的方法有时也有效,具体取决于您的需要。

    把这个放在&lt;head&gt;

    <!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
    <!--[if IE 7]>    <html class="lt-ie9 lt-ie8"> <![endif]-->
    <!--[if IE 8]>    <html class="lt-ie9"> <![endif]-->
    <!--[if gt IE 8]><!--> <html class=""> <!--<![endif]-->
    

    reference article - http://www.paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/

    【讨论】:

    • 正如一些 cmets 提到的 - 由于较新的浏览器不支持它,这越来越没用
    • modernizer 很好,但只有当给定功能在列表中时,为什么要使用多 KB 脚本来检测您明确知道只有 IE 需要它的东西,而这可以用 FAR less 检测代码(如果条件 cmets 工作,则只需几个字节
    猜你喜欢
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多