createContextualFragment method
The createContextualFragment() method enables you to parse a string of HTML into a DocumentFragment using the starting node of a Document Object Model (DOM) Range as the parsing context. Unlike markup inserted into innerHTML, this method can be used to create pre-parsed content to be added or inserted into a document when needed later.
![]() ![]() |
Syntax
var retVal = Range.createContextualFragment(fragment);Parameters
- fragment [in]
-
Type: DOMString
String containing HTML to be parsed.
- retVal [out, retval]
-
Type: DocumentFragment
A document fragment representing the parsed HTML.
Return value
Type: DocumentFragment
A document fragment representing the parsed HTML.
Exceptions
| Exception | Condition |
|---|---|
|
Throws this exception if the range has been detached. Versions earlier than Internet Explorer 10 will throw the exception as INVALID_STATE_ERR. |
Standards information
Remarks
When inserting elements into a DOM structure, some elements behave according to their context. Using createContextualFragment, you can ensure the parsed HTML will work as expected when inserted or added to the document.
Using this method in Windows Store apps using JavaScript
When you use this method in a Windows Store app using JavaScript, the fragment is filtered by the toStaticHTML method. For more info, see Making HTML safer: details for toStaticHTML.
See also

