SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot CatsEyeDesigns's Avatar
    Join Date
    Dec 2003
    0 Post(s)
    0 Thread(s)

    Dynamically determine position of an element?

    I recently saw something that I'd like to try on one of my projects, but the problem is, the technique I saw requires that I know the exact location of a certain element.

    Can you, using javascript, determine how far a table, or a table cell is from the top of the page? I could do this the old fashioned (manual) way, but this will be on several different pages, and this specific page element isn't always the same distance from the top of the page.

    Thanks in advance for any suggestions.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Alabama, USA
    0 Post(s)
    0 Thread(s)
    When you call xPageY() pass it an ID string or an element object reference. If you need NN4 support, let me know and I'll post xLayer().

    Note that table elements have their own object model - so you may run into inconsistencies. Let me know if it doesn't work.
    /* Excerpt from the X library (licensed LGPL) */
    function xPageY(e) {
      if (!(e=xGetElementById(e))) return 0;
      if (xDef(e.pageY)) return e.pageY;
      var y = 0;
      while (e) {
        if (xDef(e.offsetTop)) y += e.offsetTop;
        e = xDef(e.offsetParent) ? e.offsetParent : null;
      return y;
    function xGetElementById(e) {
      if(typeof(e)!='string') return e;
      if(document.getElementById) e=document.getElementById(e);
      else if(document.all) e=document.all[e];
      else if(document.layers) e=xLayer(e);
      else e=null;
      return e;
    function xDef() {
      for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
      return true;


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts