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 727643 of Window.getSelection()

  • Revision slug: Web/API/Window.getSelection
  • Revision title: Window.getSelection
  • Revision id: 727643
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment

Revision Content

{{ ApiRef() }}

Summary

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

Syntax

selection = window.getSelection() ;
  • selection is a {{domxref("Selection")}} object. When cast to string, either by adding empty quotes ("") or using {{jsxref("String.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 From version 9 {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

 

Specification

HTML Editing APIs

See also

Revision Source

<div>{{ ApiRef() }}</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 {{domxref("Selection")}} object. When cast to string, either by adding empty quotes (<code>""</code>) or using {{jsxref("String.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>From version 9</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><a href="https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html#selections" style="line-height: 1.5;">HTML Editing APIs</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