How to get the absolute position of an element

C

Chris Leonard

Sorry if I'm posting a similar query again but I think this explanation of
what I require is better.

I have a page which is very long and as I scroll down the page there are
various links, what I'd like to be able to do is when I click on a link
return the absolute position within the page of that link.

I've looked at event.clientY but this just gives me the offset from the top
of the current page, suppose I've scrolled down by 3 pages ?

It's driving me nuts! hope someone can help.

TIA.

Chris
 
F

Fred Basset

function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

Will return the Y position of the object on the page.

Fred Basset
(e-mail address removed)
 
C

Chris Leonard

function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

Will return the Y position of the object on the page.
Umm hang on a minute .... it's not working!

Here is what I have.

var iReturnValue = 0;
elementid=document.getElementById(layerID)
while( elementid != null ){
iReturnValue += elementid.offsetTop;
elementid = elementid.offsetParent;
}
alert("link:"+iReturnValue);

Each time I scroll down the page and click on a link I get the value 10. Why
is this ? Could it be because I have the links in a table cell and that is
the parent ? Not too sure as I'm not that hot on JavaScript, can you help
please ?

TIA

Chris
 
F

Fred Basset

I'm really sorry, but I don't know why it's not working for you. I'm
limited in the browsers I have installed at the moment, as I'm behind a
firewall, so I only have IE6 and NN7 installed, but it seems to work
fine in both of them.

Couple of things you can check.

Firstly, make sure you are passing in the id of the correct element.

Secondly, make sure that no other element on that page has the same
id/name.

Sorry I can't help further, but the code (my version AND yours) appears
to work without problems. Good luck finding the solution.

(Bear in mind that if you're using a browser other than the one above
then there may be cross-browser issues that I can't test)

Fred Basset
(e-mail address removed)
 
C

Chris Leonard

Hi

I found the problem I had put the link inside a DIV to test something else
out and forgot to remove it - Doh!

Worked fine as soon as I removed the DIV.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top