getting incorrect offsetleft and offsettop values in IE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohitchawla
    New Member
    • Jul 2007
    • 85

    getting incorrect offsetleft and offsettop values in IE

    have a problem with this code when working in IE
    it gives me 0,0 coords when i use iframe tag but gives correct coords when using firefox

    [CODE=javascript]function GetRealOffset(i d)
    {

    var elem = document.getEle mentById(id);
    var leftOffset = elem.offsetLeft ;
    var topOffset = elem.offsetTop;
    var parent = elem.offsetPare nt;

    while(parent != document.body)
    {
    leftOffset += parent.offsetLe ft;
    topOffset += parent.offsetTo p;
    parent = parent.offsetPa rent;
    }
    var Offsets = new Object();
    Offsets.top = topOffset;
    Offsets.left = leftOffset;

    alert(Offsets.t op + " " +Offsets.left)
    return Offsets;
    }[/CODE]

    i found out some more function and they r all giving me 0,0 coords
    i read in a tutorial that IE gives 0,0 coords for all elements unless the page is loaded but i want the offset values when the page is loading coz im putting advertisements in the iframes and some pages can take minutes to load.
    All these functions r working fine in mozilla tho
    i think what i need it a function to get the correct offset values when the page is loading in IE
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, rohitchawla. Welcome to TSDN!

    Check out this article.

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      Don't check against document.body, check against whether or not the offsetParent is null or not. The highest element has a null (non-existant) offsetParent.

      Comment

      Working...