views:

18

answers:

2

Is there an event for situations where something is selected on an HTML page like arbitrary text within a span element? Does such a thing exist or does one need to poll at regular interval & analyze the window.getSelection() ?

I would like to trap this event type from within a browser extension (Chrome).

A: 

yeah... there is .select of jquery that helps you. see this> http://api.jquery.com/select/

Rbacarin
hmmm... seems like one has to modify the page in order to "inject" handlers... that's not what I am asking for here.
jldupont
+1  A: 

New York Times implements some "on selection" type of functionality on all their articles without monitoring for a specific "onselection" event. Example here, highlight some text and you'll see a "?" pop up that you can use for search.

Here's the relevant code. Basically looks like onmouseup of any body text, they get the document (or window) selection and use that.

The existing "onselect" event is spec'd to only apply to input and textarea elements.

Andrew
"OnMouseUp" event on the `body` ... why look for a complex solution when there's a simple one! Thanks!!
jldupont
Well, the simple one's not quite watertight. You can make a selection without using the mouse (eg. keyboard shift+arrows in some browsers), which this approach won't catch.
bobince