Conditional comment for 'Except IE8'?

11,765

Solution 1

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>

Solution 2

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?

Share:
11,765

Related videos on Youtube

eozzy
Author by

eozzy

UI Designer &amp; Front-end Developer

Updated on April 15, 2022

Comments

  • eozzy
    eozzy about 2 years

    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

    • InfamousCup
      InfamousCup over 14 years
      The lte option will include the JS in IEs before 8.0 but not in non-IE browsers.
  • Matt
    Matt over 14 years
    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.
  • Raynos
    Raynos over 13 years
    +1 That's one heck of a clever trick with the nested comments.
  • ph33nyx
    ph33nyx almost 12 years
    I assume you could use your code with if "gte IE 8" if you want to hide it from future IE releases, right?
  • Amit Patil
    Amit Patil over 10 years
    @ph33nyx: Yes, that would be a much more usual kind of test than if !IE8.
  • mickburkejnr
    mickburkejnr almost 10 years
    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!
  • narration_sd
    narration_sd over 9 years
    That was a very good trick, Siddhartha. And the ability to put your mind back in the dust of time that is IE ;)
  • lowtechsun
    lowtechsun over 8 years
    stackoverflow.com/a/10532958/1010918 is relevant since that is needed to load the particular CSS file with js, great answer, thank you!