IE and Comments

Hi all,

I ran into an infuriating problem today which made my web page explode.

After some fussing with IE-conditional CSS, thinking I was losing my touch at getting my stuff to work in IE from the start, I started comparing two pages uses Internet Explorer’s Developer Tools, one working and one not, both very similar.

After some looking I noticed that my DOCTYPE in the broken one was winding up commented out (HTML comment). I went and looked, thinking I must have commented it out to test something and forgot, but it was commented.

Finally I realized that the HTML comment I had on the first line was causing the DOCTYPE to also be treated as being commented out.

Has anyone else ran into this problem before? I couldn’t find anything about it on Google, so I’m a bit curious.

I assume you mean “but is wasn’t commented”?

Finally I realized that the HTML comment I had on the first line was causing …

Could you clarify what you mean by “the first line”? I don’t think IE likes anything to precede the doctype, if that’s what you mean.

I did indeed mean “wasn’t”.

And yes, I meant it proceeded the DOCTYPE, so it looked something like:


<!-- an HTML comment -->
<!DOCTYPE html>

but when viewed from IE9’s Developer Tool, it looked like:


<!-- html comment -->
<!--<!DOCTYPE html>-->

which, of course, causes all manner of fun. =p

Yes, I don’t know the details, but generally it’s an absolute no no to have anything above the doctype. If you are using real XHTML you can but an xml declaration above there, but that will crash IE8 and under, though I presume it works for IE9. But as a rule, don’t put anything else up there.

That’s not in the standard though, right? That’s just an IE-r-special thing?

Okay, so yeah, I found that it is valid to have comments up there:

White space (spaces, newlines, tabs, and comments) may appear before or after each section. Sections 2 and 3 should be delimited by the HTML element.

From: The global structure of an HTML document

IE is very special. You’d think as of version 9 they could fix something this simple…

Huh. It looks like you aren’t using the standard HTML4 doctype, so maybe the same rules don’t apply? I wonder if anything changed for XHTML also. Meh, IE … can’t live with it, can’t live without it …

Oh, I am using the regular HTML 4.01 Strict DOCTYPE, I just used the short one as an example.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">