The Moose and Squirrel Files

January 1, 2009

Accessing Firefox 3 Discontinuous Selections with Javascript

Filed under: Code — Tags: , , — networknerd @ 1:02 pm

Firefox 3 now has the ability to highlight discontinuous sections of a web page by holding down the control key while making the selections. Very handy for note taking where you want to cut and paste only the relevant sections.

It occurred to me though that it might be useful to be able to access the individual parts of the selection.  My first guess was to use the window.getSelection() method and pass it an integer which refers to the part of the selection.  Bad guess, that just gave me all parts of the selection concatenated.  After referring to the documentation it seems that the selection object has the rangeCount property and the getRangeAt method for accessing individual parts of the selection.

I decided to use a bookmarklet as a quick and dirty way to check it out.  The code is listed below in a readable format or you can copy the whole url if you want to test it as is.

It’s worth pointing out that your mileage may vary here if your trying to extract a selection from an input control such as a text box.

    var sel=window.getSelection();
    var cnt=sel.rangeCount;
    var ranges=[];
    for(var i = 0; i < sel.rangeCount; i++)
      ranges[i] = sel.getRangeAt(i);

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

%d bloggers like this: