473,382 Members | 1,551 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,382 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 128398
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.