javascript 如何不在 IE 中加载脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5505155/
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
How not to load a script in IE?
提问by Daniel J F
It's possible to load a script onlyin IE with conditional comments
只能在带有条件注释的 IE 中加载脚本
<!--[if lte IE 7]>
<script type="text/javascript" src="somescript.js"></script>
<![endif]-->
but what if I don'twant to load it in IE lte 7 (but still need it in all other browsers)?
但是,如果我有什么不希望将其加载到IE 7 LTE(但仍需要它的所有其他浏览器)?
Any simple solutions?
有什么简单的解决办法吗?
P.S. I have a problem with SyntaxHighlighter - too many code slows IE7 down and since I'm short of time, I decided just to turn it off in IE7 for now.
PS 我在使用 SyntaxHighlighter 时遇到问题 - 太多代码会降低 IE7 的速度,而且由于时间不够,我决定暂时在 IE7 中将其关闭。
回答by Bala R
回答by Mike Ruhlin
<![if !IE]>
<script type="text/javascript" src="somescript.js"></script>
<![endif]>
回答by yzxben
<!--[if gte IE 7]>
<script type="text/javascript" src="somescript.js"></script>
<![endif]-->
<!--[if !IE]>
<script type="text/javascript" src="somescript.js"></script>
<![endif]-->
回答by hdg700
This syntax works good (the script wouldn't be commented in firefox, chrome and so on):
这种语法效果很好(脚本不会在 firefox、chrome 等中被注释):
<!--[if !IE]><!-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!--<![endif]-->
回答by Justin Pearce
You could try detecting the browser server-side and then echo the appropriate script includes.
您可以尝试检测浏览器服务器端,然后回显相应的脚本包含。
The following has an example on simplistic browser detection in PHP:
以下是 PHP 中简单浏览器检测的示例:
回答by MattC
I used the examples shown here and elsewhere, and it is really frustrating to see how many places this code example is messed up. Turns out the answer is simple, IE has special 'conditionals' like [if IE], but other browsers need comments to work with the 'conditionals'.
我使用了这里和其他地方显示的示例,看到这个代码示例有多少地方搞砸了,真是令人沮丧。原来答案很简单,IE 有特殊的“条件”,如 [if IE],但其他浏览器需要注释才能使用“条件”。
For example, since JQuery 2 doesn't work with IE8, you can do something like this
例如,由于 JQuery 2 不适用于 IE8,您可以执行以下操作
<!--[if IE ]> (following is only visible to IE)
<script src="./js/lib/jquery-1.6.1.min.js"></script>
<![endif]-->
<!--[if !IE]>--> (extra comment - only visible to non-IE)
<script src="./js/lib/jquery-2.1.1.min.js"></script>
<script src="./js/lib/jquery.mobile-1.4.5.min.js"></script>
<!--<![endif]-->
I have verified the above works in Firefox, Chrome, IE8, Dolphin mobile, and Chrome mobile. You can also specify version. For example, less than IE 9 would be: <!--[if lt IE 9 ]>
我已经在 Firefox、Chrome、IE8、Dolphin mobile 和 Chrome mobile 中验证了上述工作。您还可以指定版本。例如,小于 IE 9 将是:<!--[if lt IE 9 ]>
For a detailed explanation, check out http://www.sitepoint.com/web-foundations/internet-explorer-conditional-comments/
有关详细说明,请查看http://www.sitepoint.com/web-foundations/internet-explorer-conditional-comments/
回答by Vikram
Since conditional statements are not Working in IE (10,11) and only IE(11) is supported by Microsoft and if anyone is still looking at running IE specific JavaScript then this code still works tested in IE(11), and non IE browsers(Chrome,Firefox,Edge).
由于条件语句在 IE (10,11) 中不起作用,并且 Microsoft 仅支持 IE(11),如果有人仍在考虑运行 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>');}
else
{document.write('<script src="../nolng/js/ex1.js"><\/script>'); // for Chrome,Firefox,Edge}
</script>