Javascript 如何仅在 IE 中加载脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29987969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:26:39  来源:igfitidea点击:

How to load a script only in IE

javascriptinternet-explorerconditional-statements

提问by David Martins

I need a particular script to be triggered in Internet Explorer browsers Only!

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

I've tried this:

我试过这个:

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

Unfortunately this actually stops the script from being loaded.

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

EDIT: For everyone asking why I need this: IE makes scrolling extremely jumpy when using some animations. In order to address this I need to implement a script that provides smooth scrolling to IE. I don't want to apply it to other browsers as they don't need it and this script although making the scrolling smoother also makes it a bit unnatural.

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

回答by nderscore

I'm curious why you specifically need to target IE browsers, but the following code should work if that really is what you need to do:

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

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

The first half of the Regex (MSIE \d) is for detecting Internet Explorer 10 and below. The second half is for detecting IE11 (Trident.*rv:).

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

If the browser's user agent string matches that pattern, it will append somescript.jsto the page.

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

回答by vsync

currentScriptis supportedin all browsers besidesIE

currentScript除了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>

The above will conditionally append a script file only for IE browsers.

以上将有条件地附加一个仅适用于 IE 浏览器的脚本文件。

回答by Vikram

If someone still looking at running IE specific Javascript then this code still works tested in IE(11), and non IE browsers(Chrome,Firefox,Edge)

如果有人仍在考虑运行 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>

回答by Alex

You could modify this script to run your IE specific JavaScript:

您可以修改此脚本以运行特定于 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');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

回答by jcubic

You can use javascript to detect IE and insert your script dynamicaly:

您可以使用 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);
}

if you can use jQuery you can use shorter code:

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

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

Solution found in this post Check if user is using IE with jQuery

在这篇文章中找到的解决方案检查用户是否正在使用带有 jQ​​uery 的 IE

回答by Brian Muenzenmeyer

Feature detection is a better approach - I recommend looking into Modernizrto progressively enhance your solution when devices/browsers are capable of supporting it.

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

This CSS based approach sometimes works too, depending on what you need it for.

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

Put this in the <head>

把这个放在 <head>

<!--[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/

参考文章 - http://www.paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/