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.

Share:
13,607
TIMEX
Author by

TIMEX

Updated on June 04, 2022

Comments

  • TIMEX
    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
    TIMEX almost 13 years
    Thanks. Check out this guy's answer: stackoverflow.com/questions/6742087/… is it the same as yours?
  • aroth
    aroth almost 13 years
    @TIMEX - It's very similar, yes. Just some minor syntactic differences between the two, really.
  • Mardin Yadegar
    Mardin Yadegar over 10 years
    Conditional Comments are no longer supported in IE10 and above.
  • aroth
    aroth over 10 years
    That's true. There are some good suggestions here for dealing with IE10+: stackoverflow.com/questions/9900311/…
  • Raptor
    Raptor almost 10 years
    don't forget the </script>.