Conditional Comments

from Wikipedia, the free encyclopedia

Conditional Comments (short: CC ; English for: conditional comments ) are control structures in HTML documents that can only be interpreted by Microsoft Internet Explorer and programs from the Microsoft Office series (e.g. Microsoft Word ). They offer the possibility of executing different HTML depending on the program version of an HTML renderer . As of Internet Explorer 10, this form of browser switch is no longer supported for standards-compliant HTML pages .

HTML syntax

They are used in the same way as real HTML comments ( ): <!-- Kommentar -->

<!--[if 'Bedingung']>instructions<![endif]-->

Internet Explorer checks the formulated condition and interprets the content if the condition applies. Predefined comparison operators and values ​​make it possible to address different versions of the browser. Other user agents interpret this structure as a syntactically correct comment and ignore the content.

When using the standard-compliant mode, Internet Explorer 10 and higher does not offer the option of using conditional comments. The construct is then only treated as a comment.

Values ​​(selection)

value function example
! IE if no internet explorer <!--[if !IE]>
IE if Internet Explorer <!--[if IE]>
IE 5.5 if Internet Explorer version 5.5 <!--[if IE 5.5]>
IE 8 if Internet Explorer version 8 <!--[if IE 8]>
mso when Microsoft Office <!--[if mso]>
mso 15 if Microsoft Office 2013 <!--[if mso 15]>
vml if VML is supported <!--[if vml]>

If necessary, conditions can also be bracketed.

Operators

operator function example
! Non-operator <!--[if!(IE 6)]> (if not IE 6)
lt Less-than operator <!--[if lt IE 6]> (if smaller than IE 6)
lte Less than or equal to operator <!--[if lte IE 6]> (if less than or equal to IE 6)
gt Greater than operator <!--[if gt IE 6]> (if greater than IE 6)
gte Greater than or equal to operator <!--[if gte IE 6]> (if greater than or equal to IE 6)
& And operator <!--[if mso &!vml]> (if Office program without VML support)
| Or operator <!--[if mso | ie]> (if Office program or IE)

reversal

Usually, conditional comments hide instructions from user agents who do not understand them. Microsoft speaks of “downlevel-hidden”, meaning “comments hidden from older browsers”. However, this effect can also be reversed (so-called "downlevel-revealed" comments):

<![if lt IE 7]>Instructions for IE prior to version 7 and other browsers<![endif]>

This is no longer an HTML comment, but rather syntactically incorrect and therefore invalid markup. The condition in question is only evaluated by Internet Explorer. Other browsers interpret the strings <![if lt IE 7]>and <![endif]>as invalid tags and only display the content in between. Syntactically correct and valid according to W3C is the following syntax:

<!--[if lt IE 7]>-->Instructions for IE prior to version 7<!--<![endif]-->

The following variant is suitable for executing code in Internet Explorer <10 and integrating an alternative code for other browsers. Since alternative browsers cannot do anything with conditional comments, the code for the alternative browser must not be within a comment, but must be between two comments:

 <!--[IF IE]>Dieser Inhalt wird vom Internet Explorer älter als Version 10 ausgeführt<![ENDIF]-->
 <!--[IF !IE]>--> -->Dieser Inhalt wird von allen anderen Browsern ausgeführt<!-- <![ENDIF]-->

Meaning for Outlook 2007

The display of e-mails in HTML format in Outlook 2007 is no longer taken over by a component of Internet Explorer, but by Microsoft Word. In addition, various HTML features (such as forms or scripts) are not available in Outlook 2007 for security reasons. The appearance of HTML e-mail can be customized with conditional comments for Outlook, e.g. B. to offer an alternative to an input form for newsletters.

<!--[if gte mso 12]>Instructions for Office 2007 and greater<![endif]-->

JavaScript and JScript

A similar mechanism for JavaScript has existed since Internet Explorer 4 (JScript 3.0) . The JScript interpreter used in IE evaluates the following syntax, which other interpreters only recognize as a comment. Microsoft calls this behavior "conditional compilation".

 <script type="text/javascript">
 /*@cc_on @*/
 /*@if (@_jscript_version >= 5.5)
     // dieser Bereich ist für jscript-Interpreter >= v5.5 sichtbar
 @else @*/
     // dieser Bereich wird alternativ dazu ausgeführt
 /*@end @*/
 </script>

This syntax has also been included in the language scope of JScript .NET in a similar form .

See also

Web links

Individual evidence

  1. HTML5 parsing in IE10
  2. Outlook 2007 uses only Word as the e-mail editor . MSDN as of April 3, 2007.
  3. Word 2007 HTML and CSS Rendering Capabilities in Outlook 2007 (1/2) (2/2) . MSDN as of August 2006.
  4. Conditional Compilation in microsoft.com/technet
  5. @ cc_on instruction at MSDN