3

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

3 Answers3

11

You can do this using conditional comments. Example:

<!--[if IE]>
    <script src="ie.js">
<![endif]-->
<!--[if !IE]><!-->
    <script src="all.js"></script>
<!--<![endif]-->
aroth
  • 54,026
  • 20
  • 135
  • 176
  • Thanks. Check out this guy's answer: http://stackoverflow.com/questions/6742087/how-do-i-make-an-else-in-an-ie-html-conditional/6742121#6742121 is it the same as yours? – TIMEX Jul 19 '11 at 04:21
  • @TIMEX - It's very similar, yes. Just some minor syntactic differences between the two, really. – aroth Jul 19 '11 at 04:27
  • 13
    Conditional Comments are no longer supported in IE10 and above. – Mardin Yadegar Feb 24 '14 at 14:12
  • That's true. There are some good suggestions here for dealing with IE10+: http://stackoverflow.com/questions/9900311/how-do-i-target-only-internet-explorer-10-for-certain-situations-like-internet-e – aroth Feb 25 '14 at 03:40
  • don't forget the ``. – Raptor Aug 29 '14 at 02:38
1

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.

0

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).

Matteo Calò
  • 361
  • 1
  • 2
  • 15