createProcessingInstruction (W3C DOM Core method)

Spec
Version Depr. Static
DOM1 No No
Browser support (more…)
IE5.5+ FF1.5+ SA3+ OP9+
Full Full Full Buggy
Returns
ProcessingInstruction
Throws
INVALID_CHARACTER_ERR, NOT_SUPPORTED_ERR

Example

var pi = document.createProcessingInstruction
    ('xml-stylesheet', 'type="text/xml" href="test.xsl"');

The example above creates a ProcessingInstruction node, with the target xml-stylesheet and the data type="text/xml" href="test.xsl".

The end result of that operation would be this XML:

<?xml-stylesheet type="text/xml" href="test.xsl"?>

You'll notice how there's a space between the name and the data, which is required to separate them in static XML; and indeed, when we retrieve the data property of a static ProcessingInstruction node, that leading space is included. However it's not necessary to include it in the data argument of this method.

Arguments

target (DOMString) required

The target of the processing instruction.

data (DOMString) required

The data for the processing instruction.

Description

Create a ProcessingInstruction node in an XML document, with the specified target and data.

The created node can then be added to the document using Node methods such as appendChild or insertBefore.

Note: A Prolog is not a PI

The XML Prolog at the top of an XML document is not a processing instruction, even though it looks like one.

Return value

The created processing instruction node.

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
Full Full Full Full Full Full Buggy Buggy Full Buggy Buggy

The specification says that this method should throw a DOMException in HTML mode1 (code 9: NOT_SUPPORTED_ERR). Therefore if a browser supports it and does not throw an exception in that mode, this is considered a bug, and is what happens in Opera and Safari 1.3 and 2.

: Doesn’t - but needn’t - work in Internet Explorer in HTML

In Internet Explorer in HTML this method is not implemented (it returns undefined). Similarly to the previous note, this cannot be considered a bug or lack of support because ProcessingInstruction is an XML construct.

Footnotes

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

User-contributed notes

There are no comments yet.

Related Products