The Range.createContextualFragment()
method returns a DocumentFragment
by invoking the HTML fragment parsing algorithm or the XML fragment parsing algorithm with the start of the range (the parent of the selected node) as the context node. The HTML fragment parsing algorithm is used if the range belongs to a Document
whose HTMLness bit is set. In the HTML case, if the context node would be html
, for historical reasons the fragment parsing algorithm is invoked with body
as the context instead.
documentFragment = range.createContextualFragment( tagString )
var tagString = "<div>I am a div node</div>"; var range = document.createRange(); // make the parent of the first div in the document becomes the context node range.selectNode(document.getElementsByTagName("div").item(0)); var documentFragment = range.createContextualFragment(tagString); document.body.appendChild(documentFragment);
Specification | Status | Comment |
---|---|---|
DOM Parsing and Serialization The definition of 'Range.createContextualFragment()' in that specification. | Working Draft | Initial specification. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | Yes | ? | Yes | 11 | 15 | 9 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | ? | Yes | 15 | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Range/createContextualFragment