如何有条件地包含远程 JavaScript 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8085111/
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-24 04:40:52  来源:igfitidea点击:

How to include a remote JavaScript file conditionally?

javascripthtmlxhtml

提问by Linto

I have a HTML page.

我有一个 HTML 页面。

In that, according to the browser, I need to include a separate JavaScript file.

在那,根据浏览器,我需要包含一个单独的 JavaScript 文件。

How is it possible?

这怎么可能?

<script type="text/javascript">
if(navigator.appName == 'Microsoft Internet Explorer')
{
//here i need to include one.js
}
else
{
//here i need to include two.js
}

</script>

采纳答案by Dave

And finally, if you're already using JQuery in your project and just left out the tag for it, you can use $.getScript

最后,如果你已经在你的项目中使用了 JQuery 并且只是省略了它的标签,你可以使用$.getScript

回答by BNL

Here is one way, possibly not the best.

这是一种方法,可能不是最好的。

<script type="text/javascript">
if(navigator.appName == 'Microsoft Internet Explorer')
{
    document.write("<script tag here>");
}
else
{
    document.write("<other script tag here>");
}

回答by TJHeuvel

Using conditional commentsyou do some HTML only in IE.

使用条件注释只能在 IE 中执行一些 HTML。

<!--[if IE]>
 <script src='iescript.js'></script>
<![endif]-->

回答by Demian Brecht

<script type="text/javascript">
var src = navigator.appName == "Microsoft Internet Explorer" ? "one.js" : "two.js";

var script = document.createElement("script");
script.setAttribute("src", src);
document.getElementsByTagName("head")[0].appendChild(script);
</script>

Of course, you could also split the ternary operator above to your liking...

当然,您也可以根据自己的喜好拆分上面的三元运算符...

回答by Mike Christensen

If you're using jQuery, you could use getScript()

如果您使用 jQuery,则可以使用 getScript()

http://api.jquery.com/jQuery.getScript/

http://api.jquery.com/jQuery.getScript/

回答by Ryan

you can use conditional comments as outlined on http://jagregory.com/writings/using-ies-conditional-comments-for-targeted-javascript/

您可以使用http://jagregory.com/writings/using-ies-conditional-comments-for-targeted-javascript/ 上概述的条件注释

for example if you wanted to target IE versions 7 and below you could:

例如,如果您想针对 IE 7 及以下版本,您可以:

<!--[if lt IE 7]>
<script type="text/javascript" src="/js/one.js"></script>
<![endif]-->

回答by Eliseu Monar dos Santos

I recommend you to use LAB.jsor YepNope(script loaders). Both make great effort on loading external scripts the best way possible.

我建议您使用LAB.jsYepNope(脚本加载器)。两者都尽最大努力以最佳方式加载外部脚本。

For an example, using YepNope, with two conditional loads:

例如,使用 YepNope,有两个条件加载:

var agent = navigator.userAgent;
yepnope({
    test : /(msie) ([\w.]+)/.test(agent), // internet explorer
    yep  : 'ie.js',
    nope : 'other-script-if-you-want.js'
});
yepnope({
    test : /(mozilla)(?:.*? rv:([\w.]+))?/.test(agent), // firefox
    yep  : 'firefox.js'
});

回答by Alph.Dev

<script type="text/javascript">
  if(condition===true){
    document.write(unescape(
      '%3Cscript src="file1.js"%3E%3C/script%3E'+
      '%3Cscript src="file2.js"%3E%3C/script%3E'
    ));
  }
</script>

Simple and easy. 1 line per JS file.

简单易行。每个 JS 文件 1 行。