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.
Syntax
documentFragment = range.createContextualFragment(tagString)
Parameters
tagString
- Text that contains text and tags to be converted to a document fragment.
Example
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
Specification | Status | Comment |
---|---|---|
DOM Parsing and Serialization The definition of 'Range.createContextualFragment()' in that specification. |
Working Draft | Initial specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
createContextualFragment | Chrome Full support Yes | Edge Full support 12 | Firefox Full support Yes | IE Full support 11 | Opera Full support 15 | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support 14 | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.