Will Targeting Ie8 with Conditional Comments Work

Will targeting IE8 with conditional comments work?

It worked for me – both in quirks mode and in standards compliance mode. However, it does not work when switching to IE8 compatibility mode.

Conditional comment for 'Except IE8'?

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>

IE Stylsheets/ Conditional comments Not working

Here is the correct format of the comment:

<!--[if IE ]>
Special instructions for IE here
<![endif]-->

here is a NOT IE comment:

<!--[if !IE ]>
According to the conditional comment this is not IE
<![endif]-->

source: http://www.quirksmode.org/css/condcom.html

edit: just noticed that their 'not' example is wrong. i have corrected it.

Target IE 9 AND less with conditional comments

IE10 doesn't understand conditional comments, but that simply means it sees the lt IE 10 and everything else inside as one giant HTML comment, as the syntax highlighting shows.

So there is no difference between <!--[if lt IE 10]> and <!--[if lte IE 9]>: IE10 will ignore both, and IE9 will match both conditions. You could even go with <!--[if IE]> and that wouldn't be any different.

IE conditional targeting with ReactJS

Other options:

  • Use conditional compilation to write code which will only run in IE (< 11)
  • Use feature detection to detect IE and its version

Conditional compilation can easily bite you if comments are stripped during your build process, so feature detection might be less painful in the long run:

var ie = require('ie-version')
...
var className = 'customSelect'
if (ie.version && ie.version <= 9) {
className = ''
}
return <div>
<select className={className}>
...
</select>
</div>

JavaScript Conditional Comments for IE

They are HTML comments with conditional statements and operators. Let me break it down:

<!--[if gt IE 8]><!-->

The gt here means 'greater than, and IE means Internet Explored, and the version right behind. This:

<!--<![endif]-->

Means to end the conditional statement. This particular syntax differs from the regular HTML compatible syntax:

<!--[if condition]>-->
Stuff here
<!--<![endif]-->

due to the following reason, from SitePoint:

IE7 and later browsers will also reveal the -–> after the opening conditional statement. The fix suggested by Microsoft is to add an extra <! just after the opening conditional comment.

This means the --> after <!--[if condition]> is being interpreter by IE 7+ browsers. The comments you've given are used to combat this. Read more about conditional comments at Microsoft's MSDN.



Related Topics



Leave a reply



Submit