document.createProcessingInstruction

  • Revision slug: DOM/document.createProcessingInstruction
  • Revision title: document.createProcessingInstruction
  • Revision id: 62165
  • Created:
  • Creator: dbruant
  • Is current revision? No
  • Comment 3 words added, 6 words removed

Revision Content

Summary

createProcessingInstruction() creates a new processing instruction node, and returns it.

Syntax

Processing instruction node = document.createProcessingInstruction(target, data) 

Parameters

  • Processing Instruction node is a ProcessingInstruction node.
  • target refers to the target part of the processing instruction node (i.e., <?target ... ?>
  • data is a string containing the data to be added to the data within the node.

Example

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

var pi = docu.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"');

docu.insertBefore(pi, docu.firstChild);

alert(new XMLSerializer().serializeToString(docu));
// Displays: <?xml-stylsheet href="mycss.css" type="text/css"?><xml/>

Notes

  • This will only work with XML, not HTML documents (as HTML documents do not support processing instructions); attempting it on an HTML document will throw NOT_SUPPORTED_ERR.
  • Will throw a DOM_INVALID_CHARACTER_ERR exception if one tries to add an invalid processing instruction target (it should be an XML name besides any case combination of the letters 'xml') or if the closing processing instruction sequence ("?>") is added as part of the data, so unescaped user-provided data cannot be safely used without escaping or otherwise dealing with such situations.

Specification

DOM 4: createProcessingInstruction

Revision Source

<h2 name="Summary">Summary</h2>
<p><code>createProcessingInstruction()</code> creates a new processing instruction node, and returns it.</p>
<h2 name="Syntax">Syntax</h2>
<pre class="eval"><em>Processing instruction node</em> = document.createProcessingInstruction(target, data) 
</pre>
<h2 name="Parameters">Parameters</h2>
<ul> <li><code>Processing Instruction node</code> is a <a href="/en/DOM/ProcessingInstruction" title="en/DOM/ProcessingInstruction">ProcessingInstruction</a> node.</li> <li><code>target</code> refers to the target part of the processing instruction node (i.e., &lt;?<em>target</em> ... ?&gt;</li> <li><code>data</code> is a string containing the data to be added to the data within the node.</li>
</ul>
<h2 name="Example">Example</h2>
<pre>var docu = new DOMParser().parseFromString('&lt;xml&gt;&lt;/xml&gt;',  "application/xml")

var pi = docu.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"');

docu.insertBefore(pi, docu.firstChild);

alert(new XMLSerializer().serializeToString(docu));
// Displays: &lt;?xml-stylsheet href="mycss.css" type="text/css"?&gt;&lt;xml/&gt;
</pre>
<h2 name="Notes">Notes</h2>
<ul> <li>This will only work with XML, not HTML documents (as HTML documents do not support processing instructions); attempting it on an HTML document will throw <code>NOT_SUPPORTED_ERR</code>.</li> <li>Will throw a <code>DOM_INVALID_CHARACTER_ERR</code> exception if one tries to add an invalid processing instruction target (it should be an XML name besides any case combination of the letters 'xml') or if the closing processing instruction sequence ("?&gt;") is added as part of the data, so unescaped user-provided data cannot be safely used without escaping or otherwise dealing with such situations.</li>
</ul>
<h2 name="Specification">Specification</h2>
<p><a class="external" href="http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-document-createprocessinginstruction">DOM 4: createProcessingInstruction</a></p>
Revert to this revision