Conditional_comment

Conditional comment

Conditional comment

HTML code interpreted by old versions of Internet Explorer


Conditional comments are conditional statements interpreted by Microsoft Internet Explorer versions 5 through 9 in HTML source code. They can be used to provide and hide code to and from these versions of Internet Explorer. Conditional comments are not supported in Internet Explorer 10 and 11.

Conditional comments in HTML[1] first appeared in Microsoft's Internet Explorer 5 browser, although support has now been deprecated. In Internet Explorer 10, HTML conditional comments are not supported when the page is in standards mode (document mode 10).[2] JScript conditional comments were introduced in Internet Explorer 4, and they continued to be supported in Internet Explorer 10, in standards mode or compatibility mode.

Examples

Here is a simple example that demonstrates how conditional comments work.

<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js" integrity="sha512-BWbLJlfp8hzXlxT6K5KLdxPVAj+4Zn2e4FVq5P7NSFH/mkAJ18UiZRQUD4anR3jyp0/WYkeZ0Zmq5EWWrDxneQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<![endif]-->

Syntax

There are two types of "conditional comments": downlevel revealed, and downlevel hidden.

The basic syntax of each type of comment is shown in the following table. The first comment shown is the basic HTML Comment, which is included for the purpose of comparison and to illustrate the different syntax used by each type of conditional comment.

More information Comment type, Syntax or possible value ...

The HTML shown inside the syntax block in each of the conditional comments denotes any block of HTML content, including script. Both types of conditional comment use a conditional expression to indicate whether the content inside the comment block should be parsed or ignored. The conditional expression is formed from a combination of feature, operator, and/or value, depending on the feature. The following table shows the supported features and describes the values each feature supports.

More information Item, Example ...

The following table describes the operators that can be used to create conditional expressions.

More information Item, Example ...

Downlevel-hidden conditional comment

Below are two examples of a "downlevel hidden" conditional comment:

<!--[if IE 8]>
<link href="ie8only.css" rel="stylesheet">
<![endif]-->

or

<!--[if lte IE 7]>
<style type="text/css">
/* CSS here */
</style>
<![endif]-->

The directive in the first example will let IE 8 read the specified CSS file, while IE 7 or older IE versions will ignore it. Browsers other than IE will also ignore it because it looks like a standard HTML comment. The tag in the second example will let IE versions 5.0 through 7 read the internal CSS style. With different uses of this tag you can also single out IE 6, IE 5, or versions of IE that are newer (greater) or older (less) than a specified version.

Downlevel-revealed conditional comment

Below is an example of a "downlevel revealed" conditional 'comment', which is not an (X)HTML comment at all, despite the misleading name, using the default Microsoft syntax:

<![if !IE]>
<link href="non-ie.css" rel="stylesheet">
<![endif]>

This example shows content that should be exposed only to non-IE browsers, as the condition evaluates to "false" on IE (and hence the content is ignored), while the tags themselves are unrecognized (and hence ignored) on non-IE browsers. This is not valid HTML or XHTML.

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 technique[5] for downlevel-revealed conditional comments:

<!--[if !IE]>-->
<link href="non-ie.css" rel="stylesheet">
<!--<![endif]-->

While somewhat confusing in structure, this specific 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 writing code meant to display on non-IE browsers and on some versions of IE), IE will then display the --> present before the HTML content. This problem is easily solved by prepending <! to the initial --> as follows:

<!--[if gt IE 6]><!-->
This code displays on non-IE browsers and on IE 7 or higher.
<!--<![endif]-->

The extra <! is ignored by non-IE browsers, and it is also ignored by IE regardless of the condition: if false, everything within the conditional comment is ignored, and if true, the resulting tag <!--> is unrecognized and therefore ignored.

Conditional comments in JScript

Starting with Internet Explorer 4, there exists a similar proprietary mechanism for adding conditional comments within JScript, known as conditional compilation.[6]

Code examples:

<script>
/*@cc_on
  document.write("You are using IE4 or higher");
@*/
</script>

There were also several predefined variables,[7] though these cannot be relied on any longer as Microsoft altered the JScript engine of IE6 with XP SP3 and it now reports as @_jscript_version == 5.7. As a result, a possible way to detect Internet Explorer version using conditional compilation can be seen below:

<script>
/*@cc_on

  @if (@_jscript_version == 11)
    document.write("You are using IE11 with an older document mode");
  @elif (@_jscript_version == 10)
    document.write("You are using IE10");
  @elif (@_jscript_version == 9)
    document.write("You are using IE9");
  @elif (@_jscript_version == 5.8)
    document.write("You are using IE8");
  @elif (@_jscript_version == 5.7)
    document.write("You are using IE" + (!window.XMLHttpRequest ? 6 : 7));
  @elif (@_jscript_version == 5.6)
    document.write("You are using IE6");
  @elif (@_jscript_version == 5.5)
    document.write("You are using IE5.5");
  @elif (@_jscript_version < 5.5)
    document.write("You are using a version older than IE5.5");
  @else
    document.write("You are using an unknown version of IE");
  @end
 
@*/
</script>

However, conditional compilation is no longer supported in Internet Explorer 11 Standards mode.[8]

See also


References

  1. "About Conditional Comments". Microsoft Corporation. Archived from the original on 2008-10-13. Retrieved 2007-10-24.
  2. "HTML5 Parsing in IE10". Microsoft Corporation. 2011-07-06. Archived from the original on 2016-04-20.
  3. "GetProductInfo function (sysinfoapi.h)". Microsoft Corporation. 2 February 2023. Retrieved 2023-07-15.
  4. "MSDN — About Conditional Comments". Archived from the original on 2007-04-23. Retrieved 2007-01-03.
  5. "Conditional Compilation". Microsoft Corporation. Archived from the original on 2008-09-06. Retrieved 2007-12-29.
  6. "Conditional Compilation Variables". Microsoft Corporation. Archived from the original on 2012-10-17.
  7. "@cc_on Statement (JavaScript)". Microsoft Corporation. Archived from the original on 2016-04-04. Retrieved 2015-08-17.

Share this article:

This article uses material from the Wikipedia article Conditional_comment, and is written by contributors. Text is available under a CC BY-SA 4.0 International License; additional terms may apply. Images, videos and audio are available under their respective licenses.