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


piNode = document.createProcessingInstruction(target, data) 


  • piNode is a ProcessingInstruction node.
  • target refers to the first part of the processing instruction (i.e., <?target … >)
  • data is a string containing the data of the node.


Thrown if you try adding an invalid processing instruction target (it should be a valid XML name besides any case combination of the letters "xml"), or if the closing processing instruction sequence (?>) is added as part of the data.


var doc = new DOMParser().parseFromString('<xml></xml>', "application/xml")
var pi = doc.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"');

doc.insertBefore(pi, doc.firstChild);

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


DOM 4: createProcessingInstruction

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support Yes ? Yes ? ? ?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes ? Yes ? ? ?

Document Tags and Contributors

Contributors to this page: mfuji09, Tigt, fscholz, teoli, Krinkle, kscarfone, Sheppy, dbruant, Zcorpan, Brettz9
Last updated by: mfuji09,