Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way would be to use the <code>toString()</code> method of selection's Range(s), which is what <code>window.getSelection().toString()</code> is specified to do in the current version of <a href="http://html5.org/specs/dom-range.html#apis-for-the-browsing-context-selection%3a-the-selection-interface" rel="nofollow">WHATWG's new Range spec</a> (although this is <a href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=10583" rel="nofollow">contrary to what some browsers do</a> and may or may not change). The following will work with multiple selected ranges (which Mozilla supports) and also in IE &lt; 9.</p> <p>jsFiddle: <a href="http://jsfiddle.net/timdown/HkP2S/" rel="nofollow">http://jsfiddle.net/timdown/HkP2S/</a></p> <p>Code:</p> <pre><code>function getSelectionRangeText() { var selText = ""; if (window.getSelection) { var sel = window.getSelection(), rangeCount = sel.rangeCount; if (rangeCount) { for (var i = 0, rangeTexts = []; i &lt; rangeCount; ++i) { rangeTexts.push("" + sel.getRangeAt(i)); } selText = rangeTexts.join(""); } } else if (document.selection &amp;&amp; document.selection.type == "Text") { selText = document.selection.createRange().text; } return selText; } </code></pre> <p><strong>UPDATE</strong></p> <p>This solution includes text inside <code>&lt;script&gt;</code> and <code>&lt;style&gt;</code> elements. To remove this, you could use <code>cloneContents()</code> on the selection ranges and traverse the DOM of the resulting document fragments, collecting text only from text nodes not contained within <code>&lt;script&gt;</code> and <code>&lt;style&gt;</code> elements. You could also expand on this to remove text that is inside elements with CSS <code>display: none</code>.</p> <p>jsFiddle: <a href="http://jsfiddle.net/timdown/HkP2S/2/" rel="nofollow">http://jsfiddle.net/timdown/HkP2S/2/</a></p> <p>Code:</p> <pre><code>function getSelectionRangeText() { var selText = "", selTextParts = []; function getNodeText(node) { if (node.nodeType == 3) { selTextParts.push(node.data); } else if (node.hasChildNodes() &amp;&amp; !(node.nodeType == 1 &amp;&amp; /^(script|style)$/i.test(node.tagName))) { for (var child = node.firstChild; !!child; child = child.nextSibling) { getNodeText(child); } } } if (window.getSelection) { var sel = window.getSelection(), rangeCount = sel.rangeCount; if (rangeCount) { for (var i = 0; i &lt; rangeCount; ++i) { getNodeText(sel.getRangeAt(i).cloneContents()); } selText = selTextParts.join(""); } } else if (document.selection &amp;&amp; document.selection.type == "Text") { selText = document.selection.createRange().text; } return selText; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload