473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Getting the coordinates of a piece of text with Javascript?

Hi,

I would like to find the coordinates of a word inside a div or span
tag. I know you can find the coordinates of a tag inside the DOM, but
can you get the coordinates of a portion of the text inside that
element in javascript?
If the user highlights or selects text in a div or span can I get the
indicies that make up that selected piece of text? What API of
javascript do I use to find that out? Or really is there a way in
javascript to interact with the browsers text selection?
Thanks
Charlie

Feb 22 '06 #1
2 1865


sasperilla wrote:
If the user highlights or selects text in a div or span can I get the
indicies that make up that selected piece of text? What API of
javascript do I use to find that out? Or really is there a way in
javascript to interact with the browsers text selection?


Browser specific, IE/Win has
document.selection
has an object with a method createRange
var range = document.selection.createRange()
where that range has lots of properties and methods to inspect and
manipulate it:
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_selection.asp>
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_textrange.asp>

Mozilla gives you its (very different) selection object with
window.getSelection()
and that can be manipulated, partly with proprietary Mozilla stuff,
partly based on the W3C DOM Level 2 Range API (selection consists of one
or more such ranges)
<http://xulplanet.com/references/objref/Selection.html>
<http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html>
Opera 9 preview also supports
window.getSelection()
and seems to follow the Mozilla implementation though I am not sure how
complete that is currently and how complete it is intended to become.
But if text is selected somewhere in the HTML document then e.g.
window.getSelection().getRangeAt(0).deleteContents ()
does remove the selected contents.
As for selection specific events, IE has lots of them, see
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onselectstart.asp>
for instance.

Mozilla gives you two properties on the event object
rangeParent
rangeOffset
that relate to the range(s) used with the selection object.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 22 '06 #2
Does Safari have support for selection? Since this is browser specific
I might not be able to support certain browsers if I chose to use these
APIs. Cross browser support is important to me.

The only other option I can think is the use the mouse listeners (ala
drag and drop) and map those mouse clicks on to particular texts
portions and implement my own text selection. I'm not wild about that
since things like copy/paste etc wouldn't work.

Thanks
Charlie

Feb 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Need_help | last post by:
Sorry for inconveniance, but I couldn't check the newsgroup since friday. I can't see any solutions right now on my post... So, one more time... (if anyone noticed the solution)... I have...
2
by: Pallavi | last post by:
Hello, Suppose I want to highlight a piece of text. I get the x & y co-ordinates on the screen. And using dhtml I am able to highlight the text. Then I try on another machine with different...
19
by: Atif | last post by:
Hello all, In my html page I want to add an image say of 800x600. Now I want that when ever I am given two coordinates on this image say (x1, y1)=(50, 100) and (x2, y2)=(200, 300), the java script...
5
by: Kathryn | last post by:
Good morning! I am having a problem with a span. I have items, of which I only want to show the first X characters on the screen. If the user prints the page, I want the entire item to print....
1
by: Ian Davies | last post by:
I think javascript ans CSS is the solution for my problem. I wish to use them to retreive the relative coordinates of an element on webpage and store it in an text field on pressing a button. or...
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
3
by: Harch84 | last post by:
Hi I am using a javascript for google maps to geocode two locations and I thin want to retrieve the two coordinates and enter them into a Perl Script. Now I have managed to set up the page with the...
1
by: Cainnech | last post by:
Hi all, I've got a bit of a challenge. I've got a script which displays the mouse coodinates if you click on an image. Now I would like to convert these coordinates to pixelnumber. Let me see if I...
4
by: mbatestblrock | last post by:
I hope this makes some sense. My ultimate goal here is to execute a block of code if the mouse has not moved in a minute or so within the broswer. The machine I am running this on is for internal...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.