How to get the absolute position of an element

Discussion in 'Javascript' started by Chris Leonard, Aug 31, 2003.

  1. 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
     
    Chris Leonard, Aug 31, 2003
    #1
    1. Advertising

  2. Chris Leonard

    Fred Basset Guest

    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


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Fred Basset, Sep 1, 2003
    #2
    1. Advertising

  3. You are a star sir!

    "Fred Basset" <> wrote in message
    news:3f534363$0$62076$...
    > 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
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Chris Leonard, Sep 1, 2003
    #3
  4. > 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
     
    Chris Leonard, Sep 1, 2003
    #4
  5. Chris Leonard

    Fred Basset Guest

    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


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Fred Basset, Sep 2, 2003
    #5
  6. 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
     
    Chris Leonard, Sep 2, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luqman
    Replies:
    1
    Views:
    677
    Luqman
    Feb 7, 2006
  2. Microsoft Newsserver

    page element - absolute position

    Microsoft Newsserver, Apr 18, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    344
    Microsoft Newsserver
    Apr 18, 2008
  3. James Byrne
    Replies:
    3
    Views:
    624
    James Byrne
    Sep 14, 2010
  4. Stephan Koser

    Absolute position of a relative element

    Stephan Koser, Jun 14, 2004, in forum: Javascript
    Replies:
    7
    Views:
    155
    Mike Foster
    Jun 18, 2004
  5. Knut
    Replies:
    1
    Views:
    153
    George Hester
    Oct 15, 2004
Loading...

Share This Page