473,396 Members | 1,676 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,396 software developers and data experts.

How to get the absolute position of an element

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
5 128406
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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Stephan Koser | last post by:
Hi, how can I get the absolute position of a relative element? We dynamically create a page with multiple segments which are relatively ordered among each other. In these segments we have...
1
by: Luqman | last post by:
Where is Form Relative Position and Absolute Position in VS.Net 2005 ? Best Regards, Luqman
4
by: Samuel | last post by:
Hi, Is it possible to get absolute position of element with javascript? e.g. I have textbox somewhere in table. Textbox relative to table cell position is 0,0. How can I get Textbox absolute...
1
by: SolFrankRosen | last post by:
I'm trying to make a button on my form be in a fixed position, I used: <asp:Button ID="butCancel" style="position:absolute"; runat="server" Text="Cancel" /> but I get an error that this "is...
0
by: Microsoft Newsserver | last post by:
Im using IE 6/7 / ASP.NET / Javascript. I need to determine the absolute position of elements on my page client side. In summart, I need to know if an event triggered mouse position appears...
30
by: Bassem | last post by:
Hi, I've two div.s. One contains four images, when click on one, a JS function create an image corresponding to it and append it to the second div. I'm attempting to make each new image is to be...
7
by: chemlight | last post by:
I am having an issue with HTML elements not printing when positioned absolutely when they extend beyond the first page. I am working on some foreign tax refund forms. The forms are cut into multiple...
0
by: shu fenghua | last post by:
The problem is when i want to put an element("a" in my code) into an absolute positioned element and i also want this element("a") has a fixed height, then ie6 will render this element("a") as a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.