Comment (W3C DOM Core object)

Spec
Version Depr. Parent
DOM1 No CharacterData
Browser support (more…)
IE5.5+ FF1.5+ SA1.3+ OP9+
Buggy Buggy Buggy Buggy

Example

<!--Why do I have to be Mr Pink?-->

The example above shows a comment with the value Why do I have to be Mr Pink?

Description

The Comment interface inherits from CharacterData, and represents the contents of an XML or HTML comment (ie. everything between, but not including, the starting <!-- and ending -->)

A Comment node cannot have children.

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy Buggy

Internet Explorer 5.5 in HTML doesn't implement the Comment interface — it sees comments as Element nodes with the name !; consequently it doesn't inherit any properties from CharacterData, and inherits from Node as an Element.

Internet Explorer 6 and 7 in XML do not inherit the normalize method from Node.

Safari 1.3 and 2 in HTML mode1 don't implement the Comment interface, and cannot see comments at all.

All browsers treat comments that are inside <script> and <style> elements in HTML mode differently than comments inside other elements. Internet Explorer sees them as empty elements (having no child nodes, although the content can be retrieved using the proprietary innerHTML and innerText properties (including the comment markers themselves)). Firefox, Safari and Opera treat the contents of these elements as a single Text node (including the comment markers themselves, except for Opera with a <script> element, in which case the starting <!-- is not included). Although this difference in behavior is technically a bug, it matches the expectations of legacy coding practise, in which script and style information was often surrounded with an HTML comment to prevent older browsers from rendering the contents.

Footnotes

1 On XHTML or HTML pages served as text/html.

User-contributed notes

Related Products