9

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?

culebrón
  • 34,265
  • 20
  • 72
  • 110
  • 1
    That object is not in the DOM specs, as you can see on [Quirksmode.org](http://www.quirksmode.org/dom/range_intro.html#link2). That page might also be helpful for a kind of cross-browser solution. – Marcel Korpel Jun 19 '10 at 16:28
  • I'd suggest a workaround where you parse the node text for the selected text with `indexOf(selection)`, but I'm not exactly sure what you try to achieve. – FK82 Jun 24 '10 at 21:11

1 Answers1

3

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.

Tim Down
  • 318,141
  • 75
  • 454
  • 536