如何不在 IE 中加载脚本?
可以仅在带有条件注释的 IE 中加载脚本
<!--[if lte IE 7]>
<script type="text/javascript" src="somescript.js"></script>
<![endif]-->
,但是如果我不想在 IE lte 7 中加载它(但在所有其他浏览器中仍然需要它)怎么办? )?
有什么简单的解决办法吗?
PS 我对 SyntaxHighlighter 有一个问题 - 太多的代码会减慢 IE7 的速度,而且由于我时间不够,所以我决定暂时在 IE7 中将其关闭。
It's possible to load a script only in IE with conditional comments
<!--[if lte IE 7]>
<script type="text/javascript" src="somescript.js"></script>
<![endif]-->
but what if I don't want to load it in IE lte 7 (but still need it in all other browsers)?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这篇文章说您可以使用
!
(NOT) 运算符,例如[if !IE]
This post says you can use the
!
(NOT) operator like[if !IE]
这种语法效果很好(该脚本在 firefox、chrome 等中不会被注释):
This syntax works good (the script wouldn't be commented in firefox, chrome and so on):
您可以尝试检测浏览器服务器端,然后回显相应的脚本。
以下是 PHP 中简单浏览器检测的示例:
http://www.php-scripts。 com/20050912/12/
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:
http://www.php-scripts.com/20050912/12/
由于条件语句在 IE (10,11) 中不起作用,并且 Microsoft 仅支持 IE(11),如果有人仍在考虑运行 IE 特定的 JavaScript,那么此代码仍然可以在 IE(11) 和非 IE 浏览器中进行测试(Chrome、Firefox、Edge)。
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 有特殊的“条件”,如 [if IE],但其他浏览器需要注释才能使用“条件”。
例如,由于 JQuery 2 不适用于 IE8,因此您可以执行类似的操作
我已经在 Firefox、Chrome、IE8、Dolphin mobile 和 Chrome mobile 中验证了上述工作。您还可以指定版本。例如,小于 IE 9 将是:
有关详细说明,请查看 http://www.sitepoint.com/web-foundations/internet-explorer-conditional-comments/
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'.
For example, since JQuery 2 doesn't work with IE8, you can do something like this
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 ]>
For a detailed explanation, check out http://www.sitepoint.com/web-foundations/internet-explorer-conditional-comments/