[webkit-dev] [JavaScript] How to find a word user moused over

Xiaomei Ji xji at chromium.org
Thu Jul 2 10:55:18 PDT 2009


Hello,
I am trying to write a JavaScript to find the word user moused over.

FireFox supports event.rangeOffset, which is the *character* offset the
cursor is at within the element. Given English word, using this offset, I
can find the previous and next spaces and know the word under the cursor.

But seems that Webkit does not support event.rangeOffset. Then, how to find
a word user moused over? or how to convert the event.clientX to the
*character* offset within an element? My understanding is that it must
involve encoding/font/font size, but I do not have clear idea on how it
works.

Appreciate any input!

Thanks,
Xiaomei


P.S. Following example (event.rangeOffest) works in FireFox, but not
Safari/Chromium.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript">
function showToolTip(ev)
{
    if (ev.rangeOffset) {
      //alert("yes again" + ev.rangeOffset);
      document.getElementById('divCoord').title = ev.rangeOffset;
    }
}


</script></head>
<body>
<div id="divCoord"  onmouseover="showToolTip(event)">hello, how are
you. </div><!-- <div id="divCoord"
onmouseover="showToolTip(event)">广告计划</div> -->
</body></html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20090702/dab1852b/attachment.html>


More information about the webkit-dev mailing list