The use of "Conditional Comments" is a proprietary hack (i.e., not recognized as part of any Web standard) developed by Microsoft to enable customized inclusion of HTML markup when Internet Explorer (IE) is the browser. Their description provides a positive spin:

Conditional comments [...] 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.
This is a rather interesting way to suggest that other browsers are "less capable" rather than admitting that IE commonly fails standards-compliance in comparison to those "less capable" browsers such as FireFox. It also suggests that Web developers should include custom markup for an improved Web experience for IE users, hardly a very egalitarian view of the the Web as a community.

Rather than participating in a standards effort to permit some kind of similar feature, Microsoft's conditional comments feature is only defined for IE and has no facility for functioning with other browsers. One might note that according to the W3C's XML Recommendation, it is perfectly permissible for an XML processor to disregard comments, so this misuse of comments could only be promoted for use by a vendor that chooses to disregard them. While the use of conditional comments doesn't render a Web page invalid, it is what is commonly called markup abuse in that it is hiding what can be significant functionality or rendering information within a comment, in what is supposed to be ignored markup.

But enough of the rant; it's just that MS actually goes to a fair bit of trouble to explain their complete disregard for a Web standard, so this is only fair game. Given that their use is often because of the need to fix problems with IE's CSS rendering, the use of conditional comments therefore provides a ready solution.

Syntax#

The conditional comments hack takes the form of an if-then statement occurring within an HTML comment:

<!--[if IE]>
 (custom markup here)
<![endif]--> 
The condition in this case is "IE". Microsoft's documentation describes more possibilities for the condition syntax. The syntax includes possibilities for versioning and other things, such as [if gt IE 5] .

If you wish to include a custom stylesheet for IE users you can include an additional <link> element to link to it. You may wish to customize the syntax here for your own application — the following is only one possibility and should be considered as an example.

Example#

We've gone ahead and added an additional CSS stylesheet called skin_ie.css and put it in the directory containing our skin (i.e., in the same directory as skin.css and skin.js). Then, near the bottom of common_header.jsp, after the <link> to the skin.css, we add the following:

<!--[if IE]>
<link rel="stylesheet" type="text/css" media="screen,projection,print"
     href="<wiki:Link format='url' templatefile='skins/' /><c:out value='${prefs.SkinName}/skin_ie.css' />" />
<![endif]--> 
This then links in the skin_ie.css stylesheet when IE detects the conditional comment. Note that the HTML <link> element is only permitted as a child element of <head>, so if you want to use it to include an additional stylesheet via a link you should include the conditional comment between the <head> tags.

If you want to include some inline CSS to alter rendering in the document's <body> remember to balance your tags.

There are some further examples provided at http://virtuelvis.com/archives/2004/02/css-ie-only.

In our example the modified files include:

  • modified: wiki/templates/default/common_header.jsp
  • added: wiki/templates/default/skins/PlainVanilla/skin_ie.css

One final thing to remember is that you hopefully won't find yourself in the position of screwing up the rendering of non-IE browsers to fix problems in IE. You also don't want to create maintenance nightmares trying to make things work correctly in IE. This is the reason that the conditional comments used in earlier versions of JSPWiki are no longer included (this as of 2.8.x); it's a lot of work trying to keep up with Microsoft. It might be better to simply encourage your customers to use a more standards-compliant browser.



Thanks to those on the jspwiki-users mailing list who provided assistance in compiling this information.

I hope you use this knowledge for good and not for eeeevil. -- Murray Altheim

Add new attachment

In order to upload a new attachment to this page, please use the following box to find the file, then click on “Upload”.
« This page (revision-2) was last changed on 27-Sep-2008 13:31 by Murray Altheim