Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a cross-browser solution for getSelection()?

I need to make a comment mechanism in which user highlights a piece of text, clicks "comment this", and then does something. The Javascript code has to know not only the selected text (this is trivial), but also the anchorOffset, to know exactly from which to which character the text was selected.

I've found a cross-browser solution that gives you the text. Is there a reliable way to get a selection object as it's described in DOM specs?

like image 751
culebrón Avatar asked Jun 19 '10 15:06

culebrón


1 Answers

You could try IERange, which creates a selection-like object in IE and adds a getSelection() method to window. I don't think it's perfect but it's about the best standalone Range/selection library there is that I've seen.

Update

I've been working on a Range/selection library called Rangy that goes beyond what IERange provides. There's an early release available at http://code.google.com/p/rangy.

like image 136
Tim Down Avatar answered Nov 11 '22 13:11

Tim Down