Javascript - Getting the element position in IE6

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce Lee

    #1

    Javascript - Getting the element position in IE6

    I'm trying to get the position of a table on a page..

    <table id="tab">

    by using :

    var x = getAbsX(documen t.getElementByI d("tab"))
    var y = getAbsX(documen t.getElementByI d("tab"))

    which calls:

    function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt," Left"); }
    function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt," Top"); }

    function getAbsPos(elt,w hich) {
    iPos = 0;
    while (elt != null) {
    iPos += elt["offset" + which];
    elt = elt.offsetParen t;
    }
    return iPos;
    }

    but the thing keeps giving me 0's for both x and y in IE but works in
    Firefox. Can anyone help?


  • Martin Honnen

    #2
    Re: Javascript - Getting the element position in IE6



    Bruce Lee wrote:
    [color=blue]
    > I'm trying to get the position of a table on a page..
    >
    > <table id="tab">
    >
    > by using :
    >
    > var x = getAbsX(documen t.getElementByI d("tab"))
    > var y = getAbsX(documen t.getElementByI d("tab"))
    >
    > which calls:
    >
    > function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt," Left"); }
    > function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt," Top"); }
    >
    > function getAbsPos(elt,w hich) {
    > iPos = 0;
    > while (elt != null) {
    > iPos += elt["offset" + which];
    > elt = elt.offsetParen t;
    > }
    > return iPos;
    > }
    >
    > but the thing keeps giving me 0's for both x and y in IE but works in
    > Firefox.[/color]

    When are you calling the functions? Make sure that you call after the
    page has been loaded or it is at least ensured the element is rendered.

    If you still encounter problems then consider making a small test case
    and posting a URL.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Bruce Lee

      #3
      Re: Javascript - Getting the element position in IE6

      Martin

      I've put up a test case at
      The mycgiserver story: free Java servlet and JSP hosting from about 1999 to 2006, member pages at /~username/, and the classic Java tools still linked today.

      I need IE6 to correctly recognise the coordinates like Firefox does, I've
      tried different methods but no luck so far. Any help would be greatly
      appreciated - I've spent far too much time on this!


      Comment

      • RobB

        #4
        Re: Javascript - Getting the element position in IE6

        Bruce Lee wrote:[color=blue]
        > Martin
        >
        > I've put up a test case at
        > http://www.mycgiserver.com/~directcareer/js/test.html
        > I need IE6 to correctly recognise the coordinates like Firefox does,[/color]
        I've[color=blue]
        > tried different methods but no luck so far. Any help would be greatly
        > appreciated - I've spent far too much time on this![/color]

        Apparently a bit of a 'race condition' there...

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
        <html>
        <head>
        </head>
        <body>
        <table height=20><tr>< td></td></tr></table>
        <table width=500 height=100 border=1>
        <tr>
        <td width=100 bgcolor=blue>
        </td>
        <td bgcolor=yellow>
        <table width=100% id="tab" width=50 height=20 border=1 bgcolor=red>
        <tr>
        <td>

        <script type="text/javascript">

        function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt," Left");
        }
        function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt," Top");
        }

        function getAbsPos(elt,w hich) {
        iPos = 0;
        while (elt != null) {
        iPos += elt["offset" + which];
        elt = elt.offsetParen t;
        }
        return iPos;
        }

        onload=function ()
        {
        alert(

        'This red table (id:tab) is at - X:'+
        getAbsX(documen t.getElementByI d("tab"))+
        ',Y:'+getAbsY(d ocument.getElem entById("tab"))

        );
        }

        </script>
        </td>
        </tr>
        </table>
        </td>
        </tr>
        </table>
        </body>
        </html>

        Don't forget doctypes for testing like this; layout will vary with
        different ones specified.

        Comment

        • Martin Honnen

          #5
          Re: Javascript - Getting the element position in IE6



          Bruce Lee wrote:

          [color=blue]
          > I've put up a test case at
          > http://www.mycgiserver.com/~directcareer/js/test.html
          > I need IE6 to correctly recognise the coordinates like Firefox does, I've
          > tried different methods but no luck so far. Any help would be greatly
          > appreciated - I've spent far too much time on this![/color]

          As said, you need to make sure the element is completely rendered, if
          you call your functions in window.onload then you get the values you
          want but calling them while the table is not yet completely rendered is
          not reliable as you have found.

          --

          Martin Honnen
          http://JavaScript.FAQTs.com/

          Comment

          • Bruce Lee

            #6
            Re: Javascript - Getting the element position in IE6

            So would you say this is a bug in IE6? - I can't see a way of fixing it
            through normal methods.


            Comment

            • Martin Honnen

              #7
              Re: Javascript - Getting the element position in IE6



              Bruce Lee wrote:
              [color=blue]
              > So would you say this is a bug in IE6?[/color]

              Not really, if you try to find the position or dimensions of something
              the browser has not completely rendered there can be inaccuracies.
              [color=blue]
              > I can't see a way of fixing it
              > through normal methods.[/color]

              It was already suggested to use your functions when the page has been
              loaded, then IE gives you the correct values.


              --

              Martin Honnen
              http://JavaScript.FAQTs.com/

              Comment

              • Bruce Lee

                #8
                Re: Javascript - Getting the element position in IE6

                So I have to wait until all the DOM has been built to access elements? At
                what point would you say it would safe to access elements? Just before
                </html>?


                Comment

                • Martin Honnen

                  #9
                  Re: Javascript - Getting the element position in IE6



                  Bruce Lee wrote:
                  [color=blue]
                  > So I have to wait until all the DOM has been built to access elements? At
                  > what point would you say it would safe to access elements? Just before
                  > </html>?[/color]

                  As already suggested, to play safe use
                  <body onload="...">
                  respectively
                  window.onload = function (evt) {
                  ...
                  }
                  that way stuff in the page is loaded.

                  Within text/html an element is usually accessible by script after its
                  end tag has been parsed but I wouldn't necessarily expect dimension or
                  position to be reliable or fixed then.


                  --

                  Martin Honnen
                  http://JavaScript.FAQTs.com/

                  Comment

                  • Bruce Lee

                    #10
                    Re: Javascript - Getting the element position in IE6

                    The problem with that is that I need the table generated by the javascript
                    to be embedded inside a table in the middle of the page??


                    Comment

                    Working...