【发布时间】:2011-10-08 04:55:09
【问题描述】:
如果浏览器是 IE,我想包含 <script src="ie.js">。
否则,为所有其他浏览器添加all.js。
我该怎么做?
【问题讨论】:
标签: javascript html browser-detection
如果浏览器是 IE,我想包含 <script src="ie.js">。
否则,为所有其他浏览器添加all.js。
我该怎么做?
【问题讨论】:
标签: javascript html browser-detection
我一直在寻找类似的东西,到目前为止,我发现的最简单的解决方案是执行基于 JS 的条件来检查 document.documentMode 属性:https://www.w3schools.com/jsref/prop_doc_documentmode.asp
我希望这会帮助像我一样在 Google 结果中偶然发现此主题的其他人。
【讨论】:
您可以使用conditional comments 执行此操作。示例:
<!--[if IE]>
<script src="ie.js">
<![endif]-->
<!--[if !IE]><!-->
<script src="all.js"></script>
<!--<![endif]-->
【讨论】:
</script>。
条件 cmets 仅适用于 IE,因此非常适合提供仅适用于 IE 的特殊指令。从 IE 5 到 IE9(含)都支持它们。
【讨论】: