How do I include a javascript if it's IE, and another javascript if everything else?
13,607
Solution 1
You can do this using conditional comments. Example:
<!--[if IE]>
<script src="ie.js">
<![endif]-->
<!--[if !IE]><!-->
<script src="all.js"></script>
<!--<![endif]-->
Solution 2
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.
Author by
TIMEX
Updated on June 04, 2022Comments
-
TIMEX about 2 years
I want to include
<script src="ie.js">
if the browser is IE.Otherwise, include
all.js
for all other browsers.How can I do this?
-
TIMEX almost 13 yearsThanks. Check out this guy's answer: stackoverflow.com/questions/6742087/… is it the same as yours?
-
aroth almost 13 years@TIMEX - It's very similar, yes. Just some minor syntactic differences between the two, really.
-
Mardin Yadegar over 10 yearsConditional Comments are no longer supported in IE10 and above.
-
aroth over 10 yearsThat's true. There are some good suggestions here for dealing with IE10+: stackoverflow.com/questions/9900311/…
-
Raptor almost 10 yearsdon't forget the
</script>
.