Forum Moderators: open

Message Too Old, No Replies

Finding text location

         

kadnan

11:10 am on Jun 29, 2006 (gmt 0)

10+ Year Member


Sorry if I am making no sense.Is it possible to capture cordinates of a page when a certain text is selected?

Say there is a webpage of 15 lines and there is a world "How" on line 12.I selected HOW.Now i want to know cordinates of H or HOW itself.I know javascript support cordinates but was wondering whether it would work for me.?

my 2nd question is that does cordinate changes according to screen resolution?

texmex

7:46 pm on Jun 29, 2006 (gmt 0)

10+ Year Member



If you want to pick any word at random on your page, then it is highly unlikely you'll be able to find it's position using javascript.

If it's just one word in particular, you could place that word in a span element and then find the position of that.

Even this gets a tad involved as it may be inside a whole nest of elements, that themselves may have offsets within the page themselves.

This would work:
In your HTML you would have something like
<span id="howSpan">How </span>

Then in your javascript you could find that span thus:

function findMyHowThing()
{
var obj=document.getElementById("howSpan");
var curleft = obj.offsetLeft;
var curtop=obj.offsetTop;

while(obj.offsetParent)
{curleft=curleft + (obj.offsetParent.offsetLeft);
curtop=curtop + (obj.offsetParent.offsetTop);
obj=obj.offsetParent;
}

alert("your span was found at " + curleft +":" + curtop);
}

Elegant it isn't but doubt you'll find anytning simpler and reliable.

Oh and BTW. If you want to use those co-ordinates to move something over them, don't forget to add the "px" to the end of each of them.

Bernard Marx

11:18 pm on Jun 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is possible with text selections, keeping the following in mind:

1) IE only (using the "DHTML Model"). Actually, something similar may be possible using standard DOM in Moz & Opera, but if so, the actual objects and methods will be different.

2) This one uses the offset properties of the textRange [msdn.microsoft.com] object. So getting the absolute position will involve some recursion ala texmex's post above.

var tRange = document.selection.createTextRange();
alert(tRange.offsetLeft)

kadnan

11:02 am on Jul 4, 2006 (gmt 0)

10+ Year Member


Span will not help at the generation of HTML content won't be in my control since I will be fetching content from different pages.

I did find this
http://www.xulplanet.com/references/objref/Selection.html#method_extend

and then this

http://developer.mozilla.org/en/docs/DOM:range.startOffset#Example

for mozilla browsers but kinda unsure how to test in my script.

kadnan

11:14 am on Jul 5, 2006 (gmt 0)

10+ Year Member



I am using following code for my page and running in firefox but its not showing any selection.

[quirksmode.org...]

My html is given below:

[codes]
<body>
<div>
This is a text.my Name is Adan
</div>
<a href="javascript:getSel();">getRange</a>
<a href="javascript:txtDeSelection();">getRange2</a>
</body>
[/codes]

The same page returning Selection on IE

kadnan

1:11 pm on Jul 6, 2006 (gmt 0)

10+ Year Member



ok i did solve previously asked problems.I was checking startOffset,it actually resets for everyone.For instance i have text

bold text is very bold,it should be less bold
O! bold text where art thou?

and if i select "bold" in second line,startOffset doesnt tell the actual position of the starting of word rather it calculates it for each line.

Is there anyway or i would have to use charat() stuff to find the exact position?

Thanks

kadnan

11:35 am on Jul 13, 2006 (gmt 0)

10+ Year Member



I am going thru article( [w3.org...]

It does talk about startContainer and startoffset.But i am unsure and didnt get any example either that how these two can be used with each other.Forinstance,I have code:

<p>Hello World</p>
<hi>Hi you</h1>

and If i select "o" in "you",i am not sure how to get its actual position.

Please guide me in this regard.