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

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

Posted by: admin November 29, 2021 Leave a comment


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?


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.


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.