Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 822079 of Range.createContextualFragment()

  • Revision slug: Range/createContextualFragment
  • Revision title: Range.createContextualFragment()
  • Revision id: 822079
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment Web/API/range/createContextualFragment Range/createContextualFragment

Revision Content

{{ApiRef("DOM")}}{{SeeCompatTable}}

The Range.createContextualFragment() method returns a {{domxref("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
{{SpecName('DOM Parsing', '#extensions-to-the-range-interface', 'Range.createContextualFragment()')}} {{Spec2('DOM Parsing')}} Initial specification.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} 11 15.0 {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatNo}} 15.0 {{CompatUnknown}}

See also

Revision Source

<p>{{ApiRef("DOM")}}{{SeeCompatTable}}</p>

<p>The <strong><code>Range.createContextualFragment()</code></strong> method returns a {{domxref("DocumentFragment")}} by invoking the HTML fragment parsing algorithm or the XML&nbsp;fragment parsing algorithm with the start of the range (the <em>parent</em> of the selected node) as the context node. The HTML&nbsp;fragment parsing algorithm is used if the range belongs to a <code>Document</code> whose HTMLness bit is set. In the HTML case, if the context node would be <code>html</code>, for historical reasons the fragment parsing algorithm is invoked with <code>body</code> as the context instead.</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox">
<em>documentFragment</em> = <em>range</em>.createContextualFragment( <em>tagString</em> )
</pre>

<h3 id="Parameters" name="Parameters">Parameters</h3>

<dl>
 <dt><em>tagString</em></dt>
 <dd>Text that contains text and tags to be converted to a document fragment.</dd>
</dl>

<h2 id="Example" name="Example">Example</h2>

<pre class="brush: js">
var tagString = "&lt;div&gt;I am a div node&lt;/div&gt;";
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);
</pre>

<h2 id="Specification" name="Specification">Specification</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM Parsing', '#extensions-to-the-range-interface', 'Range.createContextualFragment()')}}</td>
   <td>{{Spec2('DOM Parsing')}}</td>
   <td>Initial specification.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>11</td>
   <td>15.0</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>15.0</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/DOM/DOM_Reference" title="/en-US/docs/DOM/DOM_Reference">The DOM interfaces index</a></li>
</ul>
Revert to this revision