document.createComment

  • Revision slug: DOM/document.createComment
  • Revision title: document.createComment
  • Revision id: 10353
  • Created:
  • Creator: Brettz9
  • Is current revision? No
  • Comment /* Example */ list output in example

Revision Content

{{template.DomRef()}}

Summary

createComment creates a new comment node, and returns it.

Syntax

Comment node = document.createComment(data) 

Parameters

  • Comment node is a Comment node.
  • data is a string containing the data to be added to the Comment.

Example

var docu = new DOMParser().parseFromString('<xml></xml>',  "application/xml")

var comment = docu.createComment('This is a not-so-secret comment in your document');

docu.getElementsByTagName('xml')[0].appendChild(comment);

alert(new XMLSerializer().serializeToString(docu));
// Displays: <xml><!--This is a not-so-secret comment in your document--></xml>

Notes

  • Will throw a NS_ERROR_DOM_INVALID_CHARACTER_ERR exception if one tries to include the "--" sequence as part of the data, so if based on user data, this potential string would need to be escaped or have its exception caught.

Specification

createComment

Revision Source

<p>
{{template.DomRef()}}
</p>
<h3 name="Summary">Summary</h3>
<p><b>createComment</b> creates a new comment node, and returns it. 
</p>
<h3 name="Syntax">Syntax </h3>
<pre class="eval"><i>Comment node</i> = document.createComment(data) 
</pre>
<h3 name="Parameters">Parameters </h3>
<ul><li> <code>Comment node</code> is a Comment node.
</li><li> <code>data</code> is a string containing the data to be added to the Comment.
</li></ul>
<h3 name="Example">Example </h3>
<pre>var docu = new DOMParser().parseFromString('&lt;xml&gt;&lt;/xml&gt;',  "application/xml")

var comment = docu.createComment('This is a not-so-secret comment in your document');

docu.getElementsByTagName('xml')[0].appendChild(comment);

alert(new XMLSerializer().serializeToString(docu));
// Displays: &lt;xml&gt;&lt;!--This is a not-so-secret comment in your document--&gt;&lt;/xml&gt;
</pre>
<h3 name="Notes">Notes </h3>
<ul><li> Will throw a NS_ERROR_DOM_INVALID_CHARACTER_ERR exception if one tries to include the "--" sequence as part of the data, so if based on user data, this potential string would need to be escaped or have its exception caught.
</li></ul>
<h3 name="Specification">Specification </h3>
<p><a class="external" href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#method-createComment">createComment</a>
</p>
Revert to this revision