开发者

How do I include a javascript if it's IE, and another javascript if everything else?

开发者 https://www.devze.com 2023-03-21 10:57 出处:网络
I want to in开发者_运维技巧clude <script src=\"ie.js\"> if the browser is IE. Otherwise, include all.js for all other browsers.

I want to in开发者_运维技巧clude <script src="ie.js"> if the browser is IE.

Otherwise, include all.js for all other browsers.

How can I do this?


You can do this using conditional comments. Example:

<!--[if IE]>
    <script src="ie.js">
<![endif]-->
<!--[if !IE]><!-->
    <script src="all.js"></script>
<!--<![endif]-->


I was looking for something similar, and so far the simplest solution I found is to do a JS based condition to check the document.documentMode property: https://www.w3schools.com/jsref/prop_doc_documentmode.asp

I hope this will help someone else who stumble on this thread in Google results like I did.


Conditional comments only work in IE, and are thus excellently suited to give special instructions meant only for IE. They are supported from IE 5 up until IE9 (inclusive).

0

精彩评论

暂无评论...
验证码 换一张
取 消