By definition, a code comment is text that is ignored by the translator – the browser, IE, in this case. But, the conditional comment feature adds syntax for a conditional statement that is formatted as a comment. Therefore, some text that is formatted as a comment is actually not a comment. It is markup code. Note that other browsers – that do not support the conditional comment feature – ignore them since they are formatted as comments.
An HTML code comment is text that starts with <!-- and ends with -->. An IE conditional comment is delimited the same, but is like:
The conditional comment has two forms. The one above is called downlevel hidden. The other form, called downlevel revealed, is not formatted as a comment yet is called a conditional comment none-the-less. It is formatted like:
The code between the if and endif markup can be any HTML content that is included if the condition evaluates true or excluded otherwise.
An expression can contain the name of a feature, literal values and comparison operators. Feature names include:
Literal values are either numeric or Boolean (true/false).
Comparison operators are:
A downlevel-hidden conditional comment that includes code for IE version 8:
A downlevel-hidden conditional comment that includes code for IE version 7 and less:
A downlevel-revealed conditional comment (which is not an HTML comment despite the name) that includes code if the browser is not IE.
Microsoft acknowledges this syntax is not standardized markup,4 intending these tags to be overlooked by other browsers and expose the content in the middle. In order to ensure compliance with W3C standards, some web developers use an alternative technique5 for downlevel-revealed conditional comments:
While possibly confusing, this syntax is valid (X)HTML and is useful for conditional sections intended specifically for non-IE browsers; if the condition evaluates to true (for example, if targeting non-IE browsers and on some versions of IE), IE displays the --> present before the HTML content. This problem is resolved by prepending <! to the initial --> as follows:
The extra <! is ignored by non-IE browsers, and also by IE regardless of the condition. If false, everything within the conditional comment is ignored. If true, the resulting tag <!--> is unrecognized and therefore ignored.
IE 4 introduced a similar mechanism for JScript, called conditional compilation6 that was dropped in version 11 standards mode.7
Example code:
"About Conditional Comments". Microsoft Corporation. Archived from the original on 2008-10-13. Retrieved 2007-10-24. https://web.archive.org/web/20081013121554/http://msdn2.microsoft.com/EN-US/library/ms537512.aspx ↩
"HTML5 Parsing in IE10". Microsoft Corporation. 2011-07-06. Archived from the original on 2016-04-20. https://web.archive.org/web/20160420083101/https://blogs.msdn.microsoft.com/ie/2011/07/06/html5-parsing-in-ie10/ ↩
https://archives.ecma-international.org/2007/misc/jscriptconditionalcompilation2.pdf https://archives.ecma-international.org/2007/misc/jscriptconditionalcompilation2.pdf ↩
"MSDN — About Conditional Comments". Archived from the original on 2007-04-23. Retrieved 2007-01-03. https://web.archive.org/web/20070423005446/http://msdn.microsoft.com/workshop/author/dhtml/overview/ccomment_ovw.asp ↩
"Valid downlevel-revealed conditional comments | 456 Berea Street". Archived from the original on 2014-08-19. Retrieved 2007-12-29. https://web.archive.org/web/20140819094004/http://www.456bereastreet.com/archive/200511/valid_downlevelrevealed_conditional_comments/ ↩
"Conditional Compilation". Microsoft Corporation. Archived from the original on 2008-09-06. Retrieved 2007-12-29. https://web.archive.org/web/20080906144358/http://msdn2.microsoft.com/en-us/library/ahx1z4fs(VS.80).aspx ↩
"@cc_on Statement (JavaScript)". Microsoft Corporation. Archived from the original on 2016-04-04. Retrieved 2015-08-17. https://web.archive.org/web/20160404160258/https://msdn.microsoft.com/library/8ka90k2e(v=vs.94).aspx ↩