如何不在 IE 中加载脚本?

发布于 2024-10-28 13:01:35 字数 357 浏览 8 评论 0原文

可以在带有条件注释的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无远思近则忧 2024-11-04 13:01:35

这篇文章说您可以使用! (NOT) 运算符,例如 [if !IE]

This post says you can use the ! (NOT) operator like [if !IE]

带刺的爱情 2024-11-04 13:01:35
<![if !IE]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]>
<![if !IE]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]>
面犯桃花 2024-11-04 13:01:35
<!--[if gte IE 7]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]-->
<!--[if !IE]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]-->
<!--[if gte IE 7]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]-->
<!--[if !IE]>
    <script type="text/javascript" src="somescript.js"></script>
<![endif]-->
樱花细雨 2024-11-04 13:01:35

这种语法效果很好(该脚本在 firefox、chrome 等中不会被注释):

<!--[if !IE]><!-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!--<![endif]-->

This syntax works good (the script wouldn't be commented in firefox, chrome and so on):

<!--[if !IE]><!-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!--<![endif]-->
平定天下 2024-11-04 13:01:35

您可以尝试检测浏览器服务器端,然后回显相应的脚本。

以下是 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/

半岛未凉 2024-11-04 13:01:35

由于条件语句在 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>

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).

<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>
电影里的梦 2024-11-04 13:01:35

我使用了这里和其他地方显示的示例,看到这个代码示例有多少地方被搞乱了,真是令人沮丧。事实证明答案很简单,IE 有特殊的“条件”,如 [if IE],但其他浏览器需要注释才能使用“条件”。

例如,由于 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]-->

我已经在 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

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

For a detailed explanation, check out http://www.sitepoint.com/web-foundations/internet-explorer-conditional-comments/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文