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 668799 of Range

  • Revision slug: Web/API/Range
  • Revision title: Range
  • Revision id: 668799
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment temprange Web/API/Range
Tags: 

Revision Content

{{ APIRef() }}

The Range interface represents a fragment of a document that can contain nodes and parts of text nodes in a given document.

A range can be created using the {{ domxref("Document.createRange", "createRange") }} method of the {{ domxref("Document") }} object. Range objects can also be retrieved by using the {{ domxref("Selection/getRangeAt", "getRangeAt") }} method of the {{ domxref("Selection") }} object. There also is the {{domxref("Range.Range()", "Range()")}} constructor available.

Properties

There are no inherited properties.

{{domxref("Range.collapsed")}} {{readonlyInline}}
Returns a {{domxref("Boolean")}} indicating whether the range's start and end points are at the same position.
{{domxref("Range.commonAncestorContainer")}} {{readonlyInline}}
Returns the deepest {{ domxref("Node") }} that contains the startContainer and endContainer nodes.
{{domxref("Range.endContainer")}} {{readonlyInline}}
Returns the {{ domxref("Node") }} within which the Range ends.
{{domxref("Range.endOffset")}} {{readonlyInline}}
Returns a number representing where in the endContainer the Range ends.
{{domxref("Range.startContainer")}} {{readonlyInline}}
Returns the {{ domxref("Node") }} within which the Range starts.
{{domxref("Range.startOffset")}} {{readonlyInline}}
Returns a number representing where in the startContainer the Range starts.

Constructor

{{ domxref("Range.Range()", "Range()") }} {{experimental_inline}}
Returns a Range object with the global {{domxref("Document")}} as its start and end.

Methods

There are no inherited methods.

{{ domxref("Range.setStart()")}}
Sets the start position of a Range.
{{ domxref("Range.setEnd()")}}
Sets the end position of a Range.
{{ domxref("Range.setStartBefore()")}}
Sets the start position of a Range relative to another {{ domxref("Node") }}.
{{ domxref("Range.setStartAfter()")}}
Sets the start position of a Range relative to another {{ domxref("Node") }}.
{{ domxref("Range.setEndBefore()")}}
Sets the end position of a Range relative to another {{ domxref("Node") }}.
{{ domxref("Range.setEndAfter()")}}
Sets the end position of a Range relative to another {{ domxref("Node") }}.
{{ domxref("Range.selectNode()")}}
Sets the Range to contain the {{ domxref("Node") }} and its contents.
{{ domxref("Range.selectNodeContents()")}}
Sets the Range to contain the contents of a {{ domxref("Node") }}.
{{ domxref("Range.collapse()")}}
Collapses the Range to one of its boundary points.
{{ domxref("Range.cloneContents()")}}
Returns a {{ domxref("DocumentFragment") }} copying the nodes of a Range.
{{ domxref("Range.deleteContents()")}}
Removes the contents of a Range from the {{ domxref("Document") }}.
{{ domxref("Range.extractContents()")}}
Moves contents of a Range from the document tree into a {{ domxref("DocumentFragment") }}.
{{ domxref("Range.insertNode()")}}
Insert a {{ domxref("Node") }} at the start of a Range.
{{ domxref("Range.surroundContents()")}}
Moves content of a Range into a new {{ domxref("Node") }}.
{{ domxref("Range.compareBoundaryPoints()")}}
Compares the boundary points of the Range with another Range.
{{ domxref("Range.cloneRange()")}}
Returns a Range object with boundary points identical to the cloned Range.
{{ domxref("Range.detach()")}}
Releases the Range from use to improve performance.
{{ domxref("Range.toString()")}}
Returns the text of the Range.
{{ domxref("Range.compareNode()")}} {{ Obsolete_inline }}{{non-standard_inline}}
Returns a constant representing whether the {{domxref("Node")}} is before, after, inside, or surrounding the range.
{{ domxref("Range.comparePoint()")}} {{experimental_inline}}
Returns -1, 0, or 1 indicating whether the point occurs before, inside, or after the Range.
{{ domxref("Range.createContextualFragment()")}}{{experimental_inline}}
Returns a {{ domxref("DocumentFragment") }} created from a given string of code.
{{ domxref("Range.getBoundingClientRect()") }} {{experimental_inline}}
Returns a {{ domxref("ClientRect") }} object which bounds the entire contents of the Range; this would be the union of all the rectangles returned by {{ domxref("range.getClientRects()") }}.
{{ domxref("Range.getClientRects()") }} {{experimental_inline}}
Returns a list of {{ domxref("ClientRect") }} objects that aggregates the results of {{ domxref("Element.getClientRects()") }} for all the elements in the Range.
{{ domxref("Range.intersectsNode()")}} {{experimental_inline}}
Returns a boolean indicating whether the given node intersects the Range.
{{ domxref("Range.isPointInRange()")}} {{experimental_inline}}
Returns a boolean indicating whether the given point is in the Range.

Specifications

Specification Status Comment
{{SpecName('DOM WHATWG', '#interface-range', 'Range')}} {{Spec2('DOM WHATWG')}} Do not use RangeException anymore, use DOMException instead.
Made the second parameter of collapse() optional.
Added the methods isPointInRange(), comparePoint(), and intersectsNode().
Added the constructor Range().
{{SpecName('DOM Parsing', '#extensions-to-the-range-interface', 'Extensions to Range')}} {{Spec2('DOM Parsing')}} Added the method createContextualFragment().
{{SpecName('CSSOM View', '#extensions-to-the-range-interface', 'Extensions to Range')}} {{Spec2('CSSOM View')}} Added the methods getClientRects() and getBoundingClientRect().
{{SpecName('DOM2 Traversal_Range', 'ranges.html#Level-2-Range-Interface', 'Range')}} {{Spec2('DOM2 Traversal_Range')}} Initial specification.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.0")}} [1] 9.0 9.0 {{CompatVersionUnknown}}
Range() constructor {{experimental_inline}} {{CompatVersionUnknown}} {{CompatGeckoDesktop("24.0")}} {{CompatNo}} 15.0 {{CompatVersionUnknown}}
compareNode() {{obsolete_inline}}{{non-standard_inline()}} {{CompatNo}} {{CompatGeckoDesktop("1.0")}}
Removed in {{CompatGeckoDesktop("1.9")}}
{{CompatNo}} {{CompatNo}} {{CompatNo}}
isPointInRange(), and comparePoint(){{experimental_inline}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatNo}} 15.0 {{CompatUnknown}}
intersectsNode() {{experimental_inline}} {{CompatVersionUnknown}} {{CompatGeckoDesktop("17.0")}} [2] {{CompatNo}} 15.0 {{CompatUnknown}}
getClientRects() and getBoundingClientRect(){{experimental_inline}} {{CompatVersionUnknown}} {{CompatGeckoDesktop("2.0")}} 9 15.0 5
createContextualFragment(){{experimental_inline}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatNo}} 15.0 {{CompatUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatGeckoMobile("1.0")}} [1] 9.0 9.0 {{CompatVersionUnknown}}

[1] Starting with Gecko 13.0 {{ geckoRelease("13.0") }} the Range object throws a {{ domxref("DOMException") }} as defined in DOM 4, instead of a RangeException defined in prior specifications.

[2] Gecko supported it up to Gecko 1.9, then removed it until Gecko 17 where it was reimplemented, matching the spec.

See also

Revision Source

<p>{{ APIRef() }}</p>
<p>The <strong><code>Range</code></strong> interface represents a fragment of a document that can contain nodes and parts of text nodes in a given document.</p>
<p>A range can be created using the {{ domxref("Document.createRange", "createRange") }}&nbsp;method of the&nbsp;{{ domxref("Document") }}&nbsp;object. Range objects can also be retrieved by using the {{ domxref("Selection/getRangeAt", "getRangeAt") }}&nbsp;method of the&nbsp;{{ domxref("Selection") }}&nbsp;object. There also is the {{domxref("Range.Range()", "Range()")}} constructor available.</p>
<h2 id="Properties" name="Properties">Properties</h2>
<p><em>There are no inherited properties.</em></p>
<dl>
 <dt>
  {{domxref("Range.collapsed")}} {{readonlyInline}}</dt>
 <dd>
  Returns a {{domxref("Boolean")}} indicating whether the range's start and end points are at the same position.</dd>
 <dt>
  {{domxref("Range.commonAncestorContainer")}} {{readonlyInline}}</dt>
 <dd>
  Returns the deepest&nbsp;{{ domxref("Node") }}&nbsp;that contains the <code>startContainer</code> and <code>endContainer</code> nodes.</dd>
 <dt>
  {{domxref("Range.endContainer")}} {{readonlyInline}}</dt>
 <dd>
  Returns the&nbsp;{{ domxref("Node") }}&nbsp;within which the <code>Range</code> ends.</dd>
 <dt>
  {{domxref("Range.endOffset")}} {{readonlyInline}}</dt>
 <dd>
  Returns a number representing where in the <code>endContainer</code> the <code>Range</code> ends.</dd>
 <dt>
  {{domxref("Range.startContainer")}} {{readonlyInline}}</dt>
 <dd>
  Returns the&nbsp;{{ domxref("Node") }}&nbsp;within which the <code>Range</code> starts.</dd>
 <dt>
  {{domxref("Range.startOffset")}} {{readonlyInline}}</dt>
 <dd>
  Returns a number representing where in the <code>startContainer</code> the <code>Range</code> starts.</dd>
</dl>
<h2 id="Constructor">Constructor</h2>
<dl>
 <dt>
  {{ domxref("Range.Range()", "Range()") }} {{experimental_inline}}</dt>
 <dd>
  Returns a <code>Range</code> object with the global {{domxref("Document")}} as its start and end.</dd>
</dl>
<h2 id="Methods" name="Methods">Methods</h2>
<p><em>There are no inherited methods.</em></p>
<dl>
 <dt>
  {{ domxref("Range.setStart()")}}</dt>
 <dd>
  Sets the start position of a <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.setEnd()")}}</dt>
 <dd>
  Sets the end position of a <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.setStartBefore()")}}</dt>
 <dd>
  Sets the start position of a <code>Range</code> relative to another&nbsp;{{ domxref("Node") }}.</dd>
 <dt>
  {{ domxref("Range.setStartAfter()")}}</dt>
 <dd>
  Sets the start position of a <code>Range</code> relative to another&nbsp;{{ domxref("Node") }}.</dd>
 <dt>
  {{ domxref("Range.setEndBefore()")}}</dt>
 <dd>
  Sets the end position of a <code>Range</code> relative to another&nbsp;{{ domxref("Node") }}.</dd>
 <dt>
  {{ domxref("Range.setEndAfter()")}}</dt>
 <dd>
  Sets the end position of a <code>Range</code> relative to another&nbsp;{{ domxref("Node") }}.</dd>
 <dt>
  {{ domxref("Range.selectNode()")}}</dt>
 <dd>
  Sets the <code>Range</code> to contain the&nbsp;{{ domxref("Node") }}&nbsp;and its contents.</dd>
 <dt>
  {{ domxref("Range.selectNodeContents()")}}</dt>
 <dd>
  Sets the <code>Range</code> to contain the contents of a&nbsp;{{ domxref("Node") }}.</dd>
 <dt>
  {{ domxref("Range.collapse()")}}</dt>
 <dd>
  Collapses the <code>Range</code> to one of its boundary points.</dd>
 <dt>
  {{ domxref("Range.cloneContents()")}}</dt>
 <dd>
  Returns a&nbsp;{{ domxref("DocumentFragment") }}&nbsp;copying the nodes of a <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.deleteContents()")}}</dt>
 <dd>
  Removes the contents of a <code>Range</code> from the {{ domxref("Document") }}.</dd>
 <dt>
  {{ domxref("Range.extractContents()")}}</dt>
 <dd>
  Moves contents of a <code>Range</code> from the document tree into a&nbsp;{{ domxref("DocumentFragment") }}.</dd>
 <dt>
  {{ domxref("Range.insertNode()")}}</dt>
 <dd>
  Insert a&nbsp;{{ domxref("Node") }}&nbsp;at the start of a <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.surroundContents()")}}</dt>
 <dd>
  Moves content of a <code>Range</code> into a new&nbsp;{{ domxref("Node") }}.</dd>
 <dt>
  {{ domxref("Range.compareBoundaryPoints()")}}</dt>
 <dd>
  Compares the boundary points of the <code>Range</code> with another <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.cloneRange()")}}</dt>
 <dd>
  Returns a <code>Range</code> object with boundary points identical to the cloned <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.detach()")}}</dt>
 <dd>
  Releases the <code>Range</code> from use to improve performance.</dd>
 <dt>
  {{ domxref("Range.toString()")}}</dt>
 <dd>
  Returns the text of the <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.compareNode()")}} {{ Obsolete_inline }}{{non-standard_inline}}</dt>
 <dd>
  Returns a constant representing whether the {{domxref("Node")}} is before, after, inside, or surrounding the range.</dd>
 <dt>
  {{ domxref("Range.comparePoint()")}} {{experimental_inline}}</dt>
 <dd>
  Returns -1, 0, or 1 indicating whether the point occurs before, inside, or after the <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.createContextualFragment()")}}{{experimental_inline}}</dt>
 <dd>
  Returns a&nbsp;{{ domxref("DocumentFragment") }}&nbsp;created from a given string of code.</dd>
 <dt>
  {{ domxref("Range.getBoundingClientRect()") }} {{experimental_inline}}</dt>
 <dd>
  Returns a {{ domxref("ClientRect") }} object which bounds the entire contents of the <code>Range</code>; this would be the union of all the rectangles returned by {{ domxref("range.getClientRects()") }}.</dd>
 <dt>
  {{ domxref("Range.getClientRects()") }} {{experimental_inline}}</dt>
 <dd>
  Returns a list of {{ domxref("ClientRect") }} objects that aggregates the results of {{ domxref("Element.getClientRects()") }} for all the elements in the <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.intersectsNode()")}} {{experimental_inline}}</dt>
 <dd>
  Returns a <code>boolean</code> indicating whether the given node intersects the <code>Range</code>.</dd>
 <dt>
  {{ domxref("Range.isPointInRange()")}} {{experimental_inline}}</dt>
 <dd>
  Returns a <code>boolean</code> indicating whether the given point is in the <code>Range</code>.</dd>
</dl>
<h2 id="Specifications">Specifications</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 WHATWG', '#interface-range', 'Range')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>Do not use <code>RangeException</code> anymore, use <code>DOMException</code> instead.<br />
    Made the second parameter of <code>collapse()</code> optional.<br />
    Added the methods <code>isPointInRange()</code>, <code>comparePoint()</code>, and <code>intersectsNode()</code>.<br />
    Added the constructor <code>Range()</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM Parsing', '#extensions-to-the-range-interface', 'Extensions to Range')}}</td>
   <td>{{Spec2('DOM Parsing')}}</td>
   <td>Added the method <code>createContextualFragment()</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSSOM View', '#extensions-to-the-range-interface', 'Extensions to Range')}}</td>
   <td>{{Spec2('CSSOM View')}}</td>
   <td>Added the methods <code>getClientRects()</code> and <code>getBoundingClientRect()</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Traversal_Range', 'ranges.html#Level-2-Range-Interface', 'Range')}}</td>
   <td>{{Spec2('DOM2 Traversal_Range')}}</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>{{CompatGeckoDesktop("1.0")}} [1]</td>
    <td>9.0</td>
    <td>9.0</td>
    <td>{{CompatVersionUnknown}}</td>
   </tr>
   <tr>
    <td><code>Range()</code> constructor {{experimental_inline}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatGeckoDesktop("24.0")}}</td>
    <td>{{CompatNo}}</td>
    <td>15.0</td>
    <td>{{CompatVersionUnknown}}</td>
   </tr>
   <tr>
    <td><code>compareNode()</code> {{obsolete_inline}}{{non-standard_inline()}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatGeckoDesktop("1.0")}}<br />
     Removed in {{CompatGeckoDesktop("1.9")}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
    <td>{{CompatNo}}</td>
   </tr>
   <tr>
    <td><code>isPointInRange()</code>, and <code>comparePoint()</code>{{experimental_inline}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatNo}}</td>
    <td>15.0</td>
    <td>{{CompatUnknown}}</td>
   </tr>
   <tr>
    <td><code>intersectsNode()</code> {{experimental_inline}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatGeckoDesktop("17.0")}} [2]</td>
    <td>{{CompatNo}}</td>
    <td>15.0</td>
    <td>{{CompatUnknown}}</td>
   </tr>
   <tr>
    <td><code>getClientRects()</code> and <code>getBoundingClientRect()</code>{{experimental_inline}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatGeckoDesktop("2.0")}}</td>
    <td>9</td>
    <td>15.0</td>
    <td>5</td>
   </tr>
   <tr>
    <td><code>createContextualFragment()</code>{{experimental_inline}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatVersionUnknown}}</td>
    <td>{{CompatNo}}</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>{{CompatGeckoMobile("1.0")}} [1]</td>
    <td>9.0</td>
    <td>9.0</td>
    <td>{{CompatVersionUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<p>[1] Starting with Gecko 13.0 {{ geckoRelease("13.0") }} the <code>Range</code> object throws a {{ domxref("DOMException") }} as defined in DOM 4, instead of a <code>RangeException</code> defined in prior specifications.</p>
<p>[2] Gecko supported it up to Gecko 1.9, then removed it until Gecko 17 where it was reimplemented, matching the spec.</p>
<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