6

I'm using <!--[if IE 8]><![endif]--> for targeting IE8, but there's some JS that I want to load for all browsers EXCEPT IE8, what conditional comment should I use?

Edit: I wonder if this would work: <!--[if lte IE 8]><![endif]-->

Thanks

eozzy
  • 66,048
  • 104
  • 272
  • 428

3 Answers3

20

there's some JS that I want to load for all browsers EXCEPT IE8, what conditional comment should I use?

For something to appear in ‘other browsers’ that don't support CCs, you need a downlevel-revealed conditional comment.

<!--[if !IE 8]><!-->
    ....
<!--<![endif]-->

(this is slightly different to Microsoft's official syntax which is not valid HTML.)

“All browsers except IE8” is an unusual requirement, are you sure that's what you want? What about future versions of IE?

bobince
  • 528,062
  • 107
  • 651
  • 834
14

I can think of a trick. Set a variable inside the IE conditional tag and include your JS code if that variable isn't set.

<script>
    var ie8 = false;
</script>

<!--[if IE 8]>
    <script>
        ie8 = true;
    </script>
<![endif]-->

<script>
    if (ie8 == false) {
        // any code here will not be executed by IE 8
        alert("Not IE 8!");
    }
</script>
Siddhartha Reddy
  • 6,130
  • 1
  • 33
  • 20
  • The best solution to this problem I've come across, and I should know. I've spent the whole day trying to do this with purely conditional statements! – mickburkejnr Jul 08 '14 at 15:56
  • That was a very good trick, Siddhartha. And the ability to put your mind back in the dust of time that is IE ;) – narration_sd Jan 24 '15 at 01:03
  • http://stackoverflow.com/a/10532958/1010918 is relevant since that is needed to load the particular CSS file with js, great answer, thank you! – lowtechsun Sep 30 '15 at 23:41
0

Try negation, [if !IE 8] perhaps?

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • 5
    Then IE9 comes out and you will have rather interesting behavior. Of course at this point I wouldn't be surprised if IE displayed rainbows and unicorns instead of what I intended. – Matt Nov 07 '09 at 05:56