Window.getSelection

  • Revision slug: Web/API/Window.getSelection
  • Revision title: window.getSelection
  • Revision id: 482995
  • Created:
  • Creator: Luke314
  • Is current revision? No
  • Comment Clarify, and start compatibility table.Moved From Web/API/window.getSelection to Web/API/Window.getSelection

Revision Content

{{ DomRef() }}

Summary

Returns a selection object representing the range of text selected by the user.

Syntax

selection = window.getSelection() ;
  • selection is a Selection object. When cast to string, either by adding empty quotes "" or using .toString, this object is the text selected.

Example

function foo() {
    var selObj = window.getSelection(); 
    alert(selObj);
    var selRange = selObj.getRangeAt(0);
    // do stuff with the range
}

Notes

In JavaScript, when an object is passed to a function expecting a string (like {{ Domxref("window.alert") }} or {{ Domxref("document.write") }}), the object's toString() method is called and the returned value is passed to the function. This can make the object appear to be a string when used with other functions when it is really an object with properties and methods.

In the above example, selObj.toString() is automatically called when it is passed to window.alert. However, attempting to use a JavaScript String property or method such as length or substr directly on a Selection object will result in an error if it does not have that property or method and may return unexpected results if it does. To use a Selection object as a string, call its toString method directly:

var selectedText = selObj.toString();
  • selObj is a Selection object.
  • selectedText is a string (Selected text).

Browser compatibility

{{ CompatibilityTable() }}

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

 

Specification

{{dom0()}}

It is expected to be specified in a new DOM Range spec (taken out of HTML5).

See also DOM HTML5 Range, Extensions to other interfaces

See also

Revision Source

<div>
  {{ DomRef() }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a selection object representing the range of text selected by the user. <!-- NB. this is nothing to do with the DOM selection list object! What the heck is 'selection list object'? -Nickolay --></p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<em>selection</em> = <em>window</em>.getSelection() ;</pre>
<ul>
  <li><code>selection</code> is a <a href="/en-US/docs/DOM/Selection" title="DOM/Selection">Selection</a> object. When cast to string, either by adding empty quotes "" or using .toString, this object is the text selected.</li>
</ul>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush:js">
function foo() {
    var selObj = window.getSelection(); 
    alert(selObj);
    var selRange = selObj.getRangeAt(0);
    // do stuff with the range
}</pre>
<h2 id="Notes" name="Notes">Notes</h2>
<p>In JavaScript, when an object is passed to a function expecting a string (like {{ Domxref("window.alert") }} or {{ Domxref("document.write") }}), the object's <code>toString()</code> method is called and the returned value is passed to the function. This can make the object appear to be a string when used with other functions when it is really an object with properties and methods.</p>
<p>In the above example, <code>selObj.toString()</code> is automatically called when it is passed to <code><a href="/en-US/docs/DOM/window.alert" title="DOM/window.alert">window.alert</a></code>. However, attempting to use a JavaScript <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String" title="JS/String">String</a> property or method such as <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length" title="JS/String.length">length</a></code> or <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr" title="JS/String.substr">substr</a></code> directly on a Selection object will result in an error if it does not have that property or method and may return unexpected results if it does. To use a Selection object as a string, call its <code>toString</code> method directly:</p>
<pre class="brush:js;gutter:false;">
var selectedText = selObj.toString();</pre>
<ul>
  <li><code>selObj</code> is a Selection object.</li>
  <li><code>selectedText</code> is a string (Selected text).</li>
</ul>
<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>Yes</td>
        <td>Yes</td>
        <td>Yes</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</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>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Specification" name="Specification">&nbsp;</h2>
<h2 id="Specification" name="Specification">Specification</h2>
<p>{{dom0()}}</p>
<p>It is expected to be specified in a new <a class="external" href="http://html5.org/specs/dom-range.html">DOM Range spec</a> (<a class="external" href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=10798" title="http://www.w3.org/Bugs/Public/show_bug.cgi?id=10798">taken out of HTML5</a>).</p>
<p>See also DOM HTML5 Range, <a class="external" href="http://html5.org/specs/dom-range.html#extensions-to-other-interfaces">Extensions to other interfaces</a></p>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/DOM/Selection" title="DOM/Selection">Selection</a></li>
  <li><a href="/en-US/docs/DOM/Range" title="DOM/Range">Range</a></li>
</ul>
<!--languages({
"es": "es/DOM/window.getSelection",
"fr": "fr/DOM/window.getSelection",
"it": "it/DOM/window.getSelection",
"ja": "ja/DOM/window.getSelection",
"pl": "pl/DOM/window.getSelection"
}) -->
Revert to this revision