Conditional comments are conditional statements interpreted by Microsoft
Internet Explorer
Windows Internet Explorer is a series of graphical web browsers developed by Microsoft and included as part of the Microsoft Windows line of operating systems, starting in 1995. It was first released as part of the add-on package Plus! for Windows 95 that year...
in
HTML HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....
source code. Conditional comments can be used to provide and hide code to and from Internet Explorer.
Conditional comments first appeared in
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...
's
Internet Explorer 5
Microsoft Internet Explorer 5 was a graphical web browser released in March 1999 by Microsoft, primarily for Microsoft Windows, but initially with versions available for Apple Macintosh, Sun Solaris, and HP-UX. It was one of the main participants of the first browser war...
browser and are supported through to
version 9 Windows Internet Explorer 9 is the current version of the Internet Explorer web browser from Microsoft. It was released to the public on March 14, 2011 at 21:00 PDT. Internet Explorer 9 supports several CSS 3 properties, embedded ICC v2 or v4 color profiles support via Windows Color System, and...
. Microsoft have announced support has been discontinued in
Internet Explorer 10 Windows Internet Explorer 10 is the next version of Internet Explorer currently being developed by Microsoft, and the successor to Internet Explorer 9. On April 12, 2011, Microsoft released the first "IE10 Platform Preview", which only runs on Windows 7; later platform previews only run on Windows 8...
when processing HTML5 pages, but older pages using the technique will continue to work.
Examples
Here is a simple example that demonstrates how conditional comments work.
" as follows:
Conditional comments are conditional statements interpreted by Microsoft Internet Explorer
Windows Internet Explorer is a series of graphical web browsers developed by Microsoft and included as part of the Microsoft Windows line of operating systems, starting in 1995. It was first released as part of the add-on package Plus! for Windows 95 that year...
in HTML HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....
source code. Conditional comments can be used to provide and hide code to and from Internet Explorer.
Conditional comments first appeared in Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...
's Internet Explorer 5
Microsoft Internet Explorer 5 was a graphical web browser released in March 1999 by Microsoft, primarily for Microsoft Windows, but initially with versions available for Apple Macintosh, Sun Solaris, and HP-UX. It was one of the main participants of the first browser war...
browser and are supported through to version 9 Windows Internet Explorer 9 is the current version of the Internet Explorer web browser from Microsoft. It was released to the public on March 14, 2011 at 21:00 PDT. Internet Explorer 9 supports several CSS 3 properties, embedded ICC v2 or v4 color profiles support via Windows Color System, and...
. Microsoft have announced support has been discontinued in Internet Explorer 10 Windows Internet Explorer 10 is the next version of Internet Explorer currently being developed by Microsoft, and the successor to Internet Explorer 9. On April 12, 2011, Microsoft released the first "IE10 Platform Preview", which only runs on Windows 7; later platform previews only run on Windows 8...
when processing HTML5 pages, but older pages using the technique will continue to work.
Examples
Here is a simple example that demonstrates how conditional comments work.
You are not using Internet Explorer.
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.
Comment type
Syntax or possible value
standard HTML comment
<!-- Comment content -->
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 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.
Item
Example
Comment
IE
[if IE]
The string "IE" is a feature corresponding to the version of Internet Explorer used to view the Web page.
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, see Version vector A version vector is a mechanism for tracking changes to data in a distributed system, where multiple agents might update the data at different times. The version vector allows the participants to determine if one update preceded another , followed it, or if the two updates happened concurrently...
s.
WindowsEdition
[if WindowsEdition]
Internet Explorer 8 on Windows 7. The string "WindowsEdition" is a feature corresponding to the edition of Microsoft 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 about 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
Comment
[if !IE]
The NOT operator. This is placed 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 gte 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 comment
Below are two examples of a "downlevel hidden" conditional comment:
or
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:
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, 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 for downlevel-revealed conditional comments:
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 "" as follows:
Conditional comments are conditional statements interpreted by Microsoft Internet Explorer
Windows Internet Explorer is a series of graphical web browsers developed by Microsoft and included as part of the Microsoft Windows line of operating systems, starting in 1995. It was first released as part of the add-on package Plus! for Windows 95 that year...
in HTML HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....
source code. Conditional comments can be used to provide and hide code to and from Internet Explorer.
Conditional comments first appeared in Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...
's Internet Explorer 5
Microsoft Internet Explorer 5 was a graphical web browser released in March 1999 by Microsoft, primarily for Microsoft Windows, but initially with versions available for Apple Macintosh, Sun Solaris, and HP-UX. It was one of the main participants of the first browser war...
browser and are supported through to version 9 Windows Internet Explorer 9 is the current version of the Internet Explorer web browser from Microsoft. It was released to the public on March 14, 2011 at 21:00 PDT. Internet Explorer 9 supports several CSS 3 properties, embedded ICC v2 or v4 color profiles support via Windows Color System, and...
. Microsoft have announced support has been discontinued in Internet Explorer 10 Windows Internet Explorer 10 is the next version of Internet Explorer currently being developed by Microsoft, and the successor to Internet Explorer 9. On April 12, 2011, Microsoft released the first "IE10 Platform Preview", which only runs on Windows 7; later platform previews only run on Windows 8...
when processing HTML5 pages, but older pages using the technique will continue to work.
Examples
Here is a simple example that demonstrates how conditional comments work.
You are not using Internet Explorer.
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.
Comment type
Syntax or possible value
standard HTML comment
<!-- Comment content -->
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 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.
Item
Example
Comment
IE
[if IE]
The string "IE" is a feature corresponding to the version of Internet Explorer used to view the Web page.
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, see Version vector A version vector is a mechanism for tracking changes to data in a distributed system, where multiple agents might update the data at different times. The version vector allows the participants to determine if one update preceded another , followed it, or if the two updates happened concurrently...
s.
WindowsEdition
[if WindowsEdition]
Internet Explorer 8 on Windows 7. The string "WindowsEdition" is a feature corresponding to the edition of Microsoft 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 about 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
Comment
[if !IE]
The NOT operator. This is placed 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 gte 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 comment
Below are two examples of a "downlevel hidden" conditional comment:
or
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:
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, 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 for downlevel-revealed conditional comments:
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 "" as follows:
This code displays on non-IE browsers and on IE 7 or higher.
The extra "false, everything within the conditional comment is ignored, and if true , the resulting tag
is unrecognized and therefore ignored.
While this method is functional in current versions of Internet Explorer, there is no guarantee that future versions will continue to operate this way.
Conditional comments in JScript
Starting with Internet Explorer 4, there exists a similar proprietary mechanism for adding conditional comments within JScript JScript is a scripting language based on the ECMAScript standard that is used in Microsoft's Internet Explorer.JScript is implemented as a Windows Script engine. This means that it can be "plugged in" to any application that supports Windows Script, such as Internet Explorer, Active Server Pages,...
, known as conditional compilation.
Code examples:
There were also several predefined variables, 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, the way to detect IE 6 using conditional compilation can be seen below:
The source of this article is
wikipedia , the free encyclopedia. The text of this article is licensed under the
GFDL .