根据浏览器包含不同的JavaScript文件? [英] Include different JavaScript file depending on browser?

查看:110
本文介绍了根据浏览器包含不同的JavaScript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有当浏览器不是IE浏览器时,我才想包含JavaScript文件。有没有办法做到这一点?

解决方案

您可以使用IE的有条件的评论,如下所示:

 <![if!IE]> 
< script src =your-non-IE-script.jstype =text / javascript>< / script>
<![endif]>

请注意,上述内容由非IE浏览器处理,因为条件是 not 一个HTML注释,但是一个处理指令,所以中间的位由非IE浏览器处理。 IE浏览器看到条件并跳过内容,因为它理解条件的意思是不是你,随着移动。



如果你只想做 em>对于IE,您使用的表单类似,但是使用HTML注释( - ),因为这是您可以依赖其他浏览器忽略内容的唯一方法。 IE知道要注意他们,即使他们是评论。更多关于上面的链接。



请注意,当您使用条件注释时它们会在IE(而不是其他浏览器)上显示页面加载速度(它们会暂时阻止其他资源的下载),这里更多: http://www.phpied.com/conditional-comments-block - 下载/






2013年更新:IE10 +不支持条件注释不再有。


I want to include a JavaScript file only if the browser is not IE. Is there any way to do this?

解决方案

You can do it with IE's conditional comments, like so:

<![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 not an 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."

If you want to do something only for 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.

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/


Update 2013: IE10+ don't support conditional comments anymore.

这篇关于根据浏览器包含不同的JavaScript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆