Date

I've been trying to use the findPosX() & findPosY() functions from Peter-Paul Koch's excellent 'quirksmode' site. The purpose of these functions is to find the absolute X and Y co-ordinates of an HTML element. They work well in both Internet Explorer and Firefox, but the results can be pretty rubbish on Safari.

I've made a small adjustment that fixes this problem. I've moved the loop exit from the start of the loop to the middle. Safari was failing to add in the offset of the outermost containing element. Here's the modified code:

function findPosX(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
      while(1)
      {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.x)
      curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if(obj.offsetParent)
      while(1)
      {
        curtop += obj.offsetTop;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;
      }
  else if(obj.y)
      curtop += obj.y;
  return curtop;
}

Credit

You are free to use this script for any purpose.

Peter-Paul Koch has implicitly dedicated his original version of this script to the public domain [link]. I hereby dedicate my version of the script to the public domain.

If you wish to give us credit, I suggest you just say: byPeter-Paul Koch&Alex Tingle. A link would be nice, too.