3

In IE I can do this:

var rng = document.selection.createRange();
rng.expand("word");
txt = rng.text;

How do I do the equivalent outside of IE?

Select whole word with getSelection suggested using window.getSelection().modify(), but I don't want to modify the selection.

Community
  • 1
  • 1
Sam
  • 581
  • 4
  • 17
  • Would it be possible to modify the selection (`.modify`), fetch the text and then restore the selection back? – pimvdb Oct 18 '11 at 15:11
  • I don't know. How would I restore the selection back? – Sam Oct 18 '11 at 15:17
  • 1
    Boy do I hate how microsoft is so "helpful" expanding my selection to a whole word. It makes it really difficult to select the _part_ of a word that I'm really trying to select. – Stephen P Dec 13 '11 at 02:00

2 Answers2

3

I've accepted Alexander's answer as it should work across element boundaries. I didn't need this, so the solution I actually used is below.

function GetSelectedText()
{
    var t = '';
    if (window.getSelection) // FF4 with one tab open?
    {
        var rng = window.getSelection().getRangeAt(0);
        expandtoword(rng);
        t = rng.toString();
    }
    else if (document.getSelection) // FF4 with multiple tabs open?
    {
        var rng = document.getSelection().getRangeAt(0);
        expandtoword(rng);
        t = rng.toString();
    }
    else if (document.selection) // IE8
    {
        var rng = document.selection.createRange();
        // expand range to enclose any word partially enclosed in it
        rng.expand("word");
        t = rng.text;
    }

    // convert newline chars to spaces, collapse whitespace, and trim non-word chars
    return t.replace(/\r?\n/g, " ").replace(/\s+/g, " ").replace(/^\W+|\W+$/g, '');
}

// expand FF range to enclose any word partially enclosed in it
function expandtoword(range)
{
    if (range.collapsed)
    {
        return;
    }

    while (range.startOffset > 0 && range.toString()[0].match(/\w/))
    {
        range.setStart(range.startContainer, range.startOffset - 1);
    }

    while (range.endOffset < range.endContainer.length && range.toString()[range.toString().length - 1].match(/\w/))
    {
        range.setEnd(range.endContainer, range.endOffset + 1);
    }
}
Sam
  • 581
  • 4
  • 17
0

To restore your selection, do this:

var range = selection.getRangeAt(0); // check for rangeCount in advance
var oldRange = document.createRange();
oldRange.setStart(range.startContainer, range.startOffset);
oldRange.setEnd(range.endContainer, range.endOffset);
...modify the selection and do whatever you need...
selection.removeAllRanges();
selection.addRange(oldRange);
Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93