3

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

Tobster
  • 31
  • 2

3 Answers3

1

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
  • 37,799
  • 10
  • 82
  • 91
1

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
  • 318,141
  • 75
  • 454
  • 536
  • 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 Feb 02 '10 at 12:40
  • Yes, as my answer pointed out. I described it as an option, not a recommendation. – Tim Down Feb 02 '10 at 12:45
0

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
  • 8,001
  • 14
  • 49
  • 78