Conditional Comments
- 格式:doc
- 大小:96.50 KB
- 文档页数:10
One of the most common operations performed in a Web page is to detect the
browser type and version. Browser detection is performed to ensure that the
content presented to the browser is compatible and renders correctly. The browser
type can be detected using many different techniques. Most methods of browser
detection make use of script on the server or client.
This article introduces conditional comments, which offer certain advantages over
scripted browser detection techniques. Conditional comments make it easy for
developers to take advantage of the enhanced features offered by Microsoft
Internet Explorer 5 and later versions, while writing pages that downgrade
gracefully in less-capable browsers or display correctly in browsers other than
Windows Internet Explorer. Conditional comments are the preferred means of
differentiating Cascading Style Sheets (CSS) rules intended for specific versions of
Internet Explorer.
The following topics are discussed in this document.
Terminology
Benefits of Using Conditional Comments
Syntax of Conditional Comments
o Downlevel-hidden Conditional Comments
o Downlevel-revealed Conditional Comments
Version Vectors
Examples
Related Topics
Terminology
The following terms are used in this article.
Term Description
expression A combination of operators, features, and/or values used to
form a conditional statement.
downlevel browser Any browser except Internet Explorer 5 and later versions. For
the purposes of this article, downlevel refers specifically to any
browser or browser version that does not support conditional
comments.
uplevel browser Internet Explorer 5 and later versions, which support
conditional comments. downlevel-hidden A conditional comment block that is ignored by downlevel
browsers. Internet Explorer 5 and later versions render the
HTML content if the expression evaluates to true.
downlevel-revealed A conditional comment block that is parsed by downlevel
browsers. Internet Explorer 5 and later versions also render
the HTML content if the expression evaluates to true.
Benefits of Using Conditional Comments
Conditional comments have certain advantages over scripting methods of browser
detection.
Low client-side impact.
When a downlevel browser encounters a downlevel-hidden conditional
comment, the browser skips over the HTML inside the comment, and the
content elements are not parsed, downloaded, or rendered. This saves client
machine resources.
No script required.
Conditional comments do not require scripting and DHTML, and when no
scripting is used in a Web page, no scripting engine needs to be loaded.
Conditional comments are processed during the downloading and parsing
phase, so only the content that is targeted for the browser is actually
downloaded. Conditional comments can be combined freely with other
browser detection techniques.
Separate code from detection logic.
Using conditional comments, script logic can be separated into smaller and
simpler segments of code, which are easier to maintain and understand. Plus,
code segments are loaded only by the browser version for which they were
intended.
Cross-browser.
Conditional comments have been around since Internet Explorer 5, but their
use is not restricted to Internet Explorer alone. Conditional comments can be
used to customize content delivered to browsers that support conditional
comments and those that do not. Syntax of Conditional Comments
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
downlevel-hidden
downlevel-revealed HTML
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