Conditional comments are conditional statements by Microsoft Internet Explorer versions 5 through 9 in HTML source code. Conditional comments can be used to provide Internet Explorer. Conditional comments are no longer supported in IE10 and IE11.

Conditional comments in HTML [1] first Appeared in Microsoft ‘s Internet Explorer 5 browser, media ALTHOUGH HAS-been now deprecated. In Internet Explorer 10 HTML is not supported when the page is in standard mode (document mode 10). [2] JScript conditional comments were introduced in Internet Explorer 4 and they continue to be supported in Internet Explorer 10 , in standard mode or compatibility mode.

Examples

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

<! - [if IE 6]>
<p> You are using Internet Explorer 6. </ p>
<! [endif] ->

Syntax

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

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

How kind Syntax or possible value
standard HTML how <! – How happy ->
downlevel-hidden <! – [if expression ]> HTML <! [endif] ->
downlevel-Revealed <! [if expression ]> HTML <! [endif]>

The HTML shown inside the syntax block in each of the conditional comments denotes any block of HTML content, including script. Both kinds of conditional how to use a conditional phrase to indicate indication whether the content inside the comment block shoulds 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 features and describes the values ​​each feature supports.

Item example How
IE [if IE] The string “IE” is a feature corresponding to the Internet Explorer version.
value [if IE 7] An integer or floating point numeral corresponding to the version of the browser. Returns a Boolean value of true if the version number matches the browser version. For more information, seeVersion vectors .
WindowsEdition [if WindowsEdition] Internet Explorer 8 on Windows 7. The string “WindowsEdition” is a feature Corresponding to the edition of Windows used to view the Web page.
value [if WindowsEdition 1] An integer corresponding to the edition of Windows used to view the Web page. Returns a Boolean value of true if the value matches the edition being used. For information on supported values ​​and the editions they describe, see the pdwReturnedProductType parameter of the GetProductInfo function.
true [if true] Always evaluates to true.
false [if false] Always evaluates to false.

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

Item example How
! [if! IE] The NOT operator. This is located immediately in front of the feature , operator , or subexpression to reverse the Boolean meaning of the expression.
lt [if lt IE 5.5] The less-than operator. Returns true if the first argument is less than the second argument.
lte [if lte IE 6] The less-than or equal operator. Returns true if the first argument is less than or equal to the second argument.
gt [if gt IE 5] The greater-than operator. Returns true if the first argument is greater than the second argument.
gte [if IE 7] The greater-than or equal operator. Returns true if the first argument is greater than or equal to the second argument.
() [if! (IE 7)] Subexpression operators. Used in conjunction with boolean operators to create more complex expressions.
& [if (gt IE 5) & (lt IE 7)] The AND operator. Returns true if all subexpressions evaluate to true
| [if (IE 6) | (IE 7)] The OR operator. Returns true if any of the subexpressions evaluates to true.

Downlevel-hidden conditional how

Below are two examples of a “downlevel hidden” conditional how:

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

gold

<! - [if 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 would 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. IE 6, IE 5, or versions of IE that are newer (greater) or older (less) than a specified version.

Downlevel-revealed conditional how

Below is an example of a “downlevel revealed” conditional ‘how’, which is not an (X) HTML, how does it work, despite the misleading name, using the default Microsoft syntax:

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

This example shows that it should be exposed 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, [3] intending these tags to be overlooked by other browsers and exposing the content in the middle. In order to ensure compliance with W3C standards, some web developers use a technical alternative [4] for downlevel-revealed conditional comments:

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

While somewhat confusing in structure, this specific syntax is valid (X) HTML and is useful for conditional sections 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 IE browsers and on IE 7 or higher. <! - <! [endif] ->

The extra “<!” is ignored by non-IE browsers; It is also ignored by IE regardless of the condition because false , everything within the conditional is ignored, and if true , the resulting tag <!-->is unrecognized and therefore ignored.

Conditional comments in JScript

Starting with Internet Explorer 4, there is a similar proprietary mechanism for adding conditional comments to JScript , known as conditional compilation. [5]

Code examples:

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

There have also been several predefined variables, [6] but these are not the same as those of Microsoft.

@ _jscript_version == 5.7

As a result, a possible way to detect Internet Explorer version:

< 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 && window.XMLHttpRequest)
 document.write ("You are using IE7");
 @elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 &&! window.XMLHttpRequest))
 document.write ("You are using IE6");
 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. [7]

See also

  • CSS filter
  • Conditional Comments in HTML

References

  1. Jump up^ “About Conditional Comments” . Microsoft Corporation.
  2. Jump up^ Microsoft – HTML5 Parsing in IE10
  3. Jump up^ “MSDN – About Conditional Comments” . Archived from the original on 2008-03-15 . Retrieved 2007-01-03 .
  4. Jump up^ Valid downlevel-revealed conditional comments | 456 Berea Street
  5. Jump up^ “Conditional Compilation” . Microsoft Corporation.
  6. Jump up^ Conditional Compilation Variables
  7. Jump up^ https://msdn.microsoft.com/library/8ka90k2e(v=vs.94).aspx@cc_on Statement (JavaScript)