JJ M
JJ M

Reputation:

Is it ok to put html comments outside the <html> tags?

W3c validator didn't ding me on this, but I was curious if anyone else had an opinion on placing html comments outside of the html tags?

...
</body>
</html>
<!-- byee -->

I have an application and am outputting some data and want it to be the absolute last thing that is done, which unfortunately means I've already attached my last </html>.

Upvotes: 17

Views: 3407

Answers (6)

BernardF
BernardF

Reputation: 21

FYI if you're using AngularJS and create a .directive where replace is true, a comment outside the root element in the HTML fragment will cause Angular to see two root elements and throw this error

Template for directive 'yourDirective' must have exactly one root element.

Upvotes: 1

Eric
Eric

Reputation: 11

I had an SEO company that was working on a client's site decide to add an HTML comment into one of my PHP includes that was outside the HTML tag and it caused issues in Internet Explorer. It caused a bunch of formatting issues with my drop down menus. It made no sense why it broke, but it was absolutely 100% caused by the comment. As soon as the comment was deleted all went back to normal.

Upvotes: 0

netic
netic

Reputation: 2944

Yes by all means. Any rendering engine (IE, Firefix, Opera, Safari, etc) will ignore any HTML comment tag completely regardless which position.

Upvotes: -1

Alohci
Alohci

Reputation: 82986

I don't think a comment after the </html> will cause any problems, but I believe that a comment that precedes the DOCTYPE declaration (and therefore before the <html> tag) will kick IE6 into quirks mode.

Upvotes: 15

Adam Wright
Adam Wright

Reputation: 49376

I can't see this being a problem - allowable comments are not specified in a DTD (as they're effectively for humans, not computers). Also, the DOM API (http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html) explicitly allows many comments directly under the document node (i.e. not the root HTML element, the logical document root), so any conforming browser should allow it.

This is not to say you won't find browsers or tools, especially older ones, that choke. But I'd be surprised if there were many.

Upvotes: 18

krusty.ar
krusty.ar

Reputation: 4060

Any client should completely ignore comments, so they should not cause any problems. Anyway if the validator didn't complain it's probably ok.

Upvotes: 1

Related Questions