424,303 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,303 IT Pros & Developers. It's quick & easy.

How to get the absolute position of an element

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
fr*********@whosyourdaddy.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #2

P: n/a
You are a star sir!

"Fred Basset" <fr*********@whosyourdaddy.com> wrote in message
news:3f***********************@news.frii.net...
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
fr*********@whosyourdaddy.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #3

P: n/a
> 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
Jul 20 '05 #4

P: n/a
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
fr*********@whosyourdaddy.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5

P: n/a
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
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.