Range: createContextualFragment() method
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
js
createContextualFragment(tagString)
Parameters
-
Text that contains text and tags to be converted to a document fragment.
Return value
A DocumentFragment
object.
Examples
js
const tagString = "<div>I am a div node</div>";
const range = document.createRange();
// Make the parent of the first div in the document become the context node
range.selectNode(document.getElementsByTagName("div").item(0));
const documentFragment = range.createContextualFragment(tagString);
document.body.appendChild(documentFragment);
Specifications
Specification |
---|
DOM Parsing and Serialization # ref-for-dom-range-createcontextualfragment-2 |
Browser compatibility
BCD tables only load in the browser