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