You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!

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)


Text that contains text and tags to be converted to a document fragment.


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
var documentFragment = range.createContextualFragment(tagString);


Specification Status Comment
DOM Parsing and Serialization
The definition of 'Range.createContextualFragment()' in that specification.
Working Draft Initial specification.

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Chrome Full support YesEdge Full support 12Firefox Full support YesIE Full support 11Opera Full support 15Safari Full support 9WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support 14Safari iOS Full support YesSamsung Internet Android ?


Full support  
Full support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.

See also