tags:

views:

55

answers:

3

I'm writing a browser add-on and want to get the word / words the user is currently hovering over via Javascript.

+1  A: 

There's no good general purpose function to do that. If you specify which browser you're adding on to, there might be a specific workaround. When faced with this problem in the past, I had to resort to asking the user to double-click the word (then you can detect the double-click, get selection, and reset it back).

Max Shawabkeh
+1  A: 

One option is to place every word inside its own <span> element and adding a mouseover handler to the body that checks the event's target / srcElement property to retrieve the span and therefore the word. This has significant downsides: the initial process of surrounding each word with a span could be slow; the new spans could mess up existing CSS rules; the document would end up with loads of elements that have no semantic value.

Tim Down
This works, but when you're writing a browser addon as the question suggests, which is supposed to work on arbitrary pages, inserting a boatload of tags that have no meaning to the original page, and may well break it, is not the way to go.
Max Shawabkeh
Yes, as my answer pointed out. I described it as an option, not a recommendation.
Tim Down
A: 

My guess is that you cannot get this done unless every word is in its own container like etc and you have declared a onmouseover handler for this. An easier approach would be use the currentSelection element when a user double clicks or mouse selects a piece of text on the browser. You could write on mouse click/doubleclick events for this.

Ritesh M Nayak