There’s a lot going on in the HTML Working Group mailing list right now about opt-in mechanisms for HTML5, discussions with smart people like Chris Wilson (co-chair and Microsoft IE champion) and Ian Hickson (WHATWG champion). I’ve going to sidestep the polemics for now – I’ve been trying to distill my own thoughts on that big debate, but things are still swirling around in my head because I’m still learning about the history of HTML, quirks mode, standards mode, DOCTYPEs, etc. Instead, I’m just going to write a (hopefully) quick entry on something I just figured out today.
If you are a web developer, I am sure you are very familiar with how difficult it can be to get the Internet Explorer browser to display your web pages correctly and still use web standards that are implemented more properly by other browsers. Many hundreds of people wish that this were not so, but it’s an ugly fact in web development today. IE includes a clever little feature called “Conditional Comments” that can allow you to select what elements on your page are displayed for certain versions of IE. I wish that Conditional Comments were not necessary (because they muckify my markup), but that is the state things are in right now.
The traditional way of explaining this is by some examples:
<!--[if IE]> <p>Only IE will display this paragraph</p> <![endif]-->
Figure 1 – Conditional Comments for IE
If you know your HTML (especially how HTML comments work), then it should be pretty obvious that the whole code above looks like one big comment, meaning that it will not be displayed. However, IE treats the square brackets differently by displaying the code between [if IE] and [endif] as code that it should display.
So that’s how to include code only for IE and exclude code from other browsers. Now how to do the reverse:
<![if !IE]> <p>All browsers except IE will display this paragraph</p> <![endif]-->
Figure 2 – Conditional Comments for Other Browsers (The Wrong Way)
In HTML browsers, the above code displays in every browser except IE (which will ignore everything between [if !IE] and [endif]). It seems to rely on some de-facto implementations where anything between <! and > is ignored by browsers as if it was a comment. The problem is that the above is not valid XHTML (comments in XHTML are anything between <!– and –>).
This is how I was feeding SVG to IE (in an <embed> element) and other browsers (in an <object> element). However, it always rankled me that my pages wouldn’t validate as XHTML as a result of the conditional comments.
<!--[if !IE]>--> <p>All browsers except IE will display this paragraph</p> <!--<![endif]-->
Figure 3 – Conditional Comments for Other Browsers (The Right Way)
NOTE: It took me some time to figure this out because I was putting spaces between [if !IE]> and –> and between [endif] and –>. Make sure there are no spaces and things should work. Here’s a test page so you can see how things work. There should be 4 paragraphs displayed in every browser.
Anyway, I know the other debate out there. The one about how XHTML is not worth the trouble and can be harmful if served as text/html, but I’ve already written too much today to get into that 😉