Conditional comment

Last updated

In Internet Explorer (IE) versions 5 through 9, a conditional comment is text formatted as a comment in HTML source code with special syntax that IE interprets as a conditional statement. A conditional comment specifies whether to include or exclude code based on the evaluation of a conditional expression and is generally used to support different versions of IE or a browser other than IE. [1]

Contents

Support for conditional comments was introduced in IE version 5 and dropped in version 10. In IE 10, conditional comments are not supported when the page is in standards mode (document mode 10). [2] An adjacent technology in JScript, called conditional compilation, was introduced in IE 4, and is supported in IE 10, in both standards and compatibility modes.

Syntax

By definition, a code comment is text that is ignored by the translator the browser, IE, in this case. But, the conditional comment feature adds syntax for a conditional statement that is formatted as a comment. Therefore, some text that is formatted as a comment is actually not a comment. It is markup code. Note that other browsers that do not support the conditional comment feature ignore them since they are formatted as comments.

An HTML code comment is text that starts with <!-- and ends with -->. An IE conditional comment is delimited the same, but is like:

<!-->[if expression]> ... <![endif]-->

The conditional comment has two forms. The one above is called downlevel hidden. The other form, called downlevel revealed, is not formatted as a comment yet is called a conditional comment none-the-less. It is formatted like:

<![if expression]> ... <![endif]>

The code between the if and endif markup can be any HTML content that is included if the condition evaluates true or excluded otherwise.

An expression can contain the name of a feature, literal values and comparison operators. Feature names include:

Literal values are either numeric or Boolean (true/false).

Comparison operators are:

Examples

A downlevel-hidden conditional comment that includes code for IE version 8:

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

A downlevel-hidden conditional comment that includes code for IE version 7 and less:

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

A downlevel-revealed conditional comment (which is not an HTML comment despite the name) that includes code if the browser is not IE.

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

Microsoft acknowledges this syntax is not standardized markup, [3] 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 [4] for downlevel-revealed conditional comments:

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

While possibly confusing, this 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 targeting non-IE browsers and on some versions of IE), IE displays the --> present before the HTML content. This problem is resolved 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 also by IE regardless of the condition. If false, everything within the conditional comment is ignored. If true, the resulting tag <!--> is unrecognized and therefore ignored.

Conditional compilation

IE 4 introduced a similar mechanism for JScript, called conditional compilation [5] that was dropped in version 11 standards mode. [6]

Example code:

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

See also

Related Research Articles

<span class="mw-page-title-main">HTML</span> HyperText Markup Language

Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScript, a programming language.

VBScript is a deprecated programming language for scripting on Microsoft Windows using Component Object Model (COM) based on classic Visual Basic and Active Scripting.

XSLT is a language originally designed for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text, or XSL Formatting Objects. These formats can be subsequently converted to formats such as PDF, PostScript, and PNG. Support for JSON and plain-text transformation was added in later updates to the XSLT 1.0 specification.

An HTML element is a type of HTML document component, one of several types of HTML nodes. The first used version of HTML was written by Tim Berners-Lee in 1993 and there have since been many versions of HTML. The current de facto standard is governed by the industry group WHATWG and is known as the HTML Living Standard.

Vector Markup Language (VML) is an obsolete XML-based file format for two-dimensional vector graphics. It was specified in Part 4 of the Office Open XML standards ISO/IEC 29500 and ECMA-376. According to the specification, VML is a deprecated format included in Office Open XML for legacy reasons only.

<span class="mw-page-title-main">Favicon</span> Icon associated with a particular web site

A favicon, also known as a shortcut icon, website icon, tab icon, URL icon, or bookmark icon, is a file containing one or more small icons associated with a particular website or web page. A web designer can create such an icon and upload it to a website by several means, and graphical web browsers will then make use of it. Browsers that provide favicon support typically display a page's favicon in the browser's address bar and next to the page's name in a list of bookmarks. Browsers that support a tabbed document interface typically show a page's favicon next to the page's title on the tab, and site-specific browsers use the favicon as a desktop icon.

Web standards are the formal, non-proprietary standards and other technical specifications that define and describe aspects of the World Wide Web. In recent years, the term has been more frequently associated with the trend of endorsing a set of standardized best practices for building web sites, and a philosophy of web design and development that includes those methods.

The data URI scheme is a uniform resource identifier (URI) scheme that provides a way to include data in-line in Web pages as if they were external resources. It is a form of file literal or here document. This technique allows normally separate elements such as images and style sheets to be fetched in a single Hypertext Transfer Protocol (HTTP) request, which may be more efficient than multiple HTTP requests, and used by several browser extensions to package images as well as other multimedia content in a single HTML file for page saving. As of 2024, data URIs are fully supported by all major browsers.

Microformats (μF) are predefined HTML markup created to serve as descriptive and consistent metadata about elements, designating them as representing a certain type of data. They allow software to process the information reliably by having set classes refer to a specific type of data rather than being arbitrary.

In computing, quirks mode is an approach used by web browsers to maintain backward compatibility with web pages designed for old web browsers, instead of strictly complying with web standards in standards mode. This behavior has since been codified, so what was previously standards mode is now referred to as simply no quirks mode.

Tableless web design is a web design method that avoids the use of HTML tables for page layout control purposes. Instead of HTML tables, style sheet languages such as Cascading Style Sheets (CSS) are used to arrange elements and text on a web page.

In computing, the two primary stylesheet languages are Cascading Style Sheets (CSS) and the Extensible Stylesheet Language (XSL). While they are both called stylesheet languages, they have very different purposes and ways of going about their tasks.

A CSS hack is a coding technique used to hide or show CSS markup depending on the browser, version number, or capabilities. Browsers have different interpretations of CSS behavior and different levels of support for the W3C standards. CSS hacks are sometimes used to achieve consistent layout appearance in multiple browsers that do not have compatible rendering. Most of these hacks do not work in modern versions of the browsers, and other techniques, such as feature support detection, have become more prevalent.

Haml is a templating system that is designed to avoid writing inline code in a web document and make the HTML cleaner. Similar to other template systems like eRuby, Haml also embeds some code that gets executed during runtime and generates HTML code in order to provide some dynamic content. In order to run Haml code, files need to have a .haml extension. These files are similar to .erb or .eRuby files, which also help embed Ruby code while developing a web application.

Extensible HyperText Markup Language (XHTML) is part of the family of XML markup languages which mirrors or extends versions of the widely used HyperText Markup Language (HTML), the language in which Web pages are formulated.

XPath is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) in 1999, and can be used to compute values from the content of an XML document. Support for XPath exists in applications that support XML, such as web browsers, and many programming languages.

<span class="mw-page-title-main">CSS</span> Style sheet language

Cascading Style Sheets (CSS) is a style sheet language used for specifying the presentation and styling of a document written in a markup language such as HTML or XML. CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript.

XHTML+RDFa is an extended version of the XHTML markup language for supporting RDF through a collection of attributes and processing rules in the form of well-formed XML documents. XHTML+RDFa is one of the techniques used to develop Semantic Web content by embedding rich semantic markup. Version 1.1 of the language is a superset of XHTML 1.1, integrating the attributes according to RDFa Core 1.1. In other words, it is an RDFa support through XHTML Modularization.

The cache manifest in HTML5 was a software storage feature which provided the ability to access a web application even without a network connection. It became part of the W3C Recommendation on 28 October 2014.

jQuery Mobile is a touch-optimized web framework, specifically a JavaScript library, developed by the jQuery project team. The development focuses on creating a framework compatible with many smartphones and tablet computers, made necessary by the growing but heterogeneous tablet and smartphone market. The jQuery Mobile framework is consistent with other mobile app frameworks and platforms such as PhoneGap, Worklight, etc.

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. "MSDN — About Conditional Comments". Archived from the original on 2007-04-23. Retrieved 2007-01-03.
  4. "Valid downlevel-revealed conditional comments | 456 Berea Street". Archived from the original on 2014-08-19. Retrieved 2007-12-29.
  5. "Conditional Compilation". Microsoft Corporation. Archived from the original on 2008-09-06. Retrieved 2007-12-29.
  6. "@cc_on Statement (JavaScript)". Microsoft Corporation. Archived from the original on 2016-04-04. Retrieved 2015-08-17.