根据浏览器包含不同的 JavaScript 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3688769/
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
Include different JavaScript file depending on browser?
提问by skroth
I want to include a JavaScript file only if the browser is not IE. Is there any way to do this?
仅当浏览器不是 IE 时,我才想包含一个 JavaScript 文件。有没有办法做到这一点?
回答by T.J. Crowder
You can do it with IE's conditional comments, like so:
您可以使用 IE 的条件注释来实现,如下所示:
<![if !IE]>
<script src="your-non-IE-script.js" type="text/javascript"></script>
<![endif]>
Note that the above is processed by non-IE browsers because the conditional is notan HTML comment, but a processing instruction,so the bit in the middle is processed by non-IE browsers. IE sees the conditional and skips over the content because it understands the conditional means "Not you, move along."
注意上面是非IE浏览器处理的,因为conditional不是HTML注释,而是处理指令,所以中间的位是非IE浏览器处理的。IE 看到条件并跳过内容,因为它理解条件的意思是“不是你,继续前进”。
If you want to do something onlyfor IE, you use a form that's similar, but uses HTML comments instead (with the --) because that's the only way you can rely on other browsers ignoring the contents. IE knows to pay attention to them, even though they're comments. More on the link above.
如果你想要做的事只对IE浏览器,您使用的形式很相似,但使用HTML注释,而不是(与--),因为这是你可以依靠其他浏览器忽略了内容的唯一途径。IE 知道要注意它们,即使它们是注释。更多关于上面的链接。
Note that there's a page load speed implication on IE (not the other browsers) when you use conditional comments (they temporarily block download of other resources), more here: http://www.phpied.com/conditional-comments-block-downloads/
请注意,当您使用条件注释(它们暂时阻止其他资源的下载)时,IE(而不是其他浏览器)会影响页面加载速度,更多信息请访问:http: //www.phpied.com/conditional-comments-block-下载/
Update 2013: IE10+ don't support conditional comments anymore.
2013 年更新:IE10+ 不再支持条件注释。
回答by Brian Campbell
You can use an IE conditional comment.
您可以使用IE 条件注释。
To get something that will show up in browsers other than IE, but not in IE, and which will still validate, you can use:
要获得将在 IE 以外的浏览器中显示但不在 IE 中显示且仍将验证的内容,您可以使用:
<!--[if !IE]>-->
<script src="..."></script>
<!--<![endif]-->
回答by Josh Leitzel
First, a note: This isn't really a good practice. If possible, you should strive to design your website in a browser-agnostic way, so that it works consistently across all browsers without the need to maintain hacks and tricks for browser-specific problems.
首先,请注意:这不是一个很好的做法。如果可能,您应该努力以与浏览器无关的方式设计您的网站,以便它在所有浏览器上都能一致地工作,而无需为特定于浏览器的问题维护黑客和技巧。
But if you really want to do this, it's easiest to include a file only if the browser isIE:
但如果你真的想这样做,最简单的方法是仅当浏览器是IE时才包含一个文件:
<!--[if lt IE 7]>
<script type="text/javascript" src="global.js"></script>
<![endif]-->
(Includes the file only if the browser is IE6 or less.)
(仅当浏览器为 IE6 或更低版本时才包含文件。)
However, if you really want to do it the other way around, here are some of your options:
但是,如果您真的想反其道而行之,这里有一些选择:
- Use server-side browser sniffing to process the browser before the page is drawn. That is, use a server-side language (Java, PHP, whatever) to determine what the browser is (usually through the user agent string) and then conditionally include your JS files that way. (For example, you can use PHP's
get_browserfunction.) - Use client-side browser sniffing to call another JS file if the browser is not IE. You can determine the browser using JavaScript itself, and then insert another JS file into the page if the browser is anything but IE. (For example, you can use jQuery's
browserfunction.) - T.J.'s answerprovides a way of doing it with I.E.'s conditional comments as well.
- 在绘制页面之前使用服务器端浏览器嗅探来处理浏览器。也就是说,使用服务器端语言(Java、PHP 等)来确定浏览器是什么(通常通过用户代理字符串),然后有条件地以这种方式包含您的 JS 文件。(例如,您可以使用 PHP 的
get_browser函数。) - 如果浏览器不是 IE,则使用客户端浏览器嗅探调用另一个 JS 文件。您可以使用 JavaScript 本身来确定浏览器,如果浏览器不是 IE,则在页面中插入另一个 JS 文件。(例如,您可以使用 jQuery 的
browser功能。) - TJ 的回答也提供了一种使用 IE 的条件注释来做到这一点的方法。
回答by Mark Snidovich
It's lesser known than conditional comments, but I thought I'd mention that you can also use an IE feature called conditional compilation - see http://msdn.microsoft.com/en-us/library/7kx09ct1%28VS.80%29.aspx
它比条件注释鲜为人知,但我想我会提到您还可以使用称为条件编译的 IE 功能 - 请参阅http://msdn.microsoft.com/en-us/library/7kx09ct1%28VS.80%29 .aspx
This example comes from their docs:
这个例子来自他们的文档:
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
document.write("JScript Version 5.0 or better.<BR>");
@else @*/
document.write("You need a more recent script engine.<BR>");
/*@end @*/
回答by mkoistinen
Yes, conditional scripts for IE is your answer:
是的,IE 的条件脚本就是您的答案:
<!--[if lt IE 7 ]>
<script src="/js/my_ie_script.js"></script>
<![endif]-->
回答by Tomasz Kowalczyk
You can do that totally different way [inserting something ONLY if IE] using Conditional Comments, maybe that would help you.
您可以使用条件注释以完全不同的方式[仅在 IE 时插入某些内容],也许这会对您有所帮助。
回答by supakeen
How about turning it around? If this is a possibility you can use IE's conditional comments: http://www.quirksmode.org/css/condcom.html
转过来怎么样?如果这是一种可能性,您可以使用 IE 的条件注释:http: //www.quirksmode.org/css/condcom.html
Otherwise you could sniff user-agents but this is considered 'bad practice'.
否则你可以嗅探用户代理,但这被认为是“不好的做法”。

