views:

200

answers:

4

For example, if I have:

<div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur urna felis, convallis quis, placerat in, elementum quis, libero. Nam quis lacus. Vivamus rhoncus quam et metus. Praesent et velit eget sem vestibulum volutpat. Integer sed risus. Integer quis libero id diam bibendum luctus. Donec eleifend. Curabitur ut sem. Praesent at est ac sem rhoncus interdum. Etiam arcu nulla, molestie dictum, mollis sed, imperdiet sit amet, neque. Fusce at nibh sit amet mi eleifend aliquam. Nunc tristique scelerisque risus. Praesent et velit id magna volutpat volutpat.</div>

...and then it's loaded in the browser and I'm hovering my mouse over various words, is there any reasonable way to detect which word is being hovered over? Any unreasonable way?

A: 

I don't think it's possible. You'd have to surround each word in some kind of identifiable tag like a span.

Chris
+2  A: 

The unreasonable way involves calculating from the x,y coordinates of the mouse and then counting in an internal map of the paragraph. This will of course break when the next user uses a different font/size.

Look up those annoying intellitag ad popups that come when you rollover a keyword. You will notice that they are added after the page has loaded by a javascript that reads across every word and replaces important ones with surrounded spans.

Karl
Counting in an internal map of the paragraph? I'm not sure I see what you mean...
Oh, perhaps you mean if I was in control of the server. No, I'm looking for an easy way to select and save sentences as I browse the web. Whatever turns out to have the best UI will be used in one or more of Greasemonkey script/Chickenfoot script/Firefox addon.
I think we all assumed you wanted server-side control, and that's why it is so hard. On the client, it's a million times easier, rightclick menus all know what text is selected, don't they?
Karl
I never seem to learn that I need to give absolutely complete specs. Ultimately, I don't need just the selected word, I need the selected sentence. And since I'll be selecting many, many sentences, I want to be able to click once anywhere in the sentence and programmatically detect it from there.
A: 

I have no idea if this helps or not since I haven't looked at the code, I would check the NY times. I say this because I believe they have the site set up so you can double click on any word and get the definition. This might be a place to start.

Kevin
This may be based on selected text instead of mouseover.
EndangeredMassa
It is -- when the selection changes, the button appears.
strager
+3  A: 

This has to be heavy, but it works ( jQuery ) :

// highlight every word found in a <p>
$("p").each(
    function () { 
        var content = "";    
        var words = $(this).html().match(/\W*\w+/g) ;
        var in_tag = false ;
        for (i in words) {
            if (words[i].match(/^\W*</)) {
                in_tag = true ;
            }
            if (words[i].match(/^\W*>/)) {
                in_tag = false ;
            }
            if (in_tag) {
                content += words[i];
            } else {
                content += words[i].replace(/(\w+)/,'<span class="word">$1</span>');
            }
        }
        $(this).html(content);
    }
);

// example event

$(".word").mouseover(function() { $(this).css("background-color","#FF0") });
$(".word").mouseout(function() { $(this).css("background-color","") });

You may want to replace $("p") with $("#mydiv").

EDIT : I wrote that there is a bug breaking tags, but tags are not the problem here ; the problem is that this code does not cope with HTML entities. In the current page, for example, when processing &lt;div&gt;, "gt" and "lt" are transformed like they were words by themselves, which is indeed wrong. Regexps would have to be a little more complicated to fix that bug.

vincent