offsetLeft/Top Bug in Firefox? (value is 8)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Thornhill

    offsetLeft/Top Bug in Firefox? (value is 8)

    I'm trying to calculate the top and left distance from the side of the
    browser of an object (either absolutely or relatively positioned) and i'm
    running into a weird problem as i'm trying to make the code cross-browser.

    normally i simply use the .offsetLeft/Top properties of the object to get my
    numbers, and it is as simple as that. but in Firefox (v. 0.9.3), they both
    return the value "8" no matter where the object appears on the page.

    i've tried many ways to make this work, and always with the same results.

    my current proof of concept page (included below) contains two methods (one
    direct and one recursive) which both work in IE (v. 6) and Opera (v. 7.54)

    any help to get this working in Firefox would be greatly appreciated.

    thanks,
    keith

    ----------------------------------------------------
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=iso-8859-1">
    <title>Untitl ed Document</title>
    </head>

    <body>
    <table align="center" id="table" border="1">
    <tr>
    <td>test</td>
    </tr>
    </table>
    <br>
    <br>
    table is
    <script>
    var left;
    left = document.getEle mentById("table ").offsetLe ft;
    document.write( left);

    document.write( " and ");

    var Element = document.getEle mentById("table ");
    var CalculatedTotal OffsetLeft;
    CalculatedTotal OffsetLeft = 0;
    while (Element.offset Parent) {
    CalculatedTotal OffsetLeft += Element.offsetL eft ;
    Element = Element.offsetP arent ;
    }
    document.write( CalculatedTotal OffsetLeft);
    </script>
    px away from left of screen.<br>
    <br>
    </body>
    </html>
    ----------------------------------------------------


  • Martin Honnen

    #2
    Re: offsetLeft/Top Bug in Firefox? (value is 8)



    Keith Thornhill wrote:
    [color=blue]
    > I'm trying to calculate the top and left distance from the side of the
    > browser of an object (either absolutely or relatively positioned) and i'm
    > running into a weird problem as i'm trying to make the code cross-browser.
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    > "http://www.w3.org/TR/html4/loose.dtd">
    > <html>
    > <head>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=iso-8859-1">
    > <title>Untitl ed Document</title>
    > </head>
    >
    > <body>
    > <table align="center" id="table" border="1">
    > <tr>
    > <td>test</td>
    > </tr>
    > </table>
    > <br>
    > <br>
    > table is
    > <script>
    > var left;
    > left = document.getEle mentById("table ").offsetLe ft;
    > document.write( left);
    >
    > document.write( " and ");
    >
    > var Element = document.getEle mentById("table ");
    > var CalculatedTotal OffsetLeft;
    > CalculatedTotal OffsetLeft = 0;
    > while (Element.offset Parent) {
    > CalculatedTotal OffsetLeft += Element.offsetL eft ;
    > Element = Element.offsetP arent ;
    > }
    > document.write( CalculatedTotal OffsetLeft);
    > </script>[/color]

    What happens if you wait for the load event to fire and then check the
    offset values in window.onload (not using document.write obviously)?
    Does that give you the values you are looking for?

    --

    Martin Honnen


    Comment

    • Keith Thornhill

      #3
      Re: offsetLeft/Top Bug in Firefox? (value is 8)

      When using the following code, I get the same results.

      --------------------------------------------------
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html;
      charset=iso-8859-1">
      <title>Untitl ed Document</title>
      </head>

      <body onLoad="check() ">
      <table align="center" id="table" border="1">
      <tr>
      <td>test</td>
      </tr>
      </table>
      <br>
      <br>
      <script>
      function check() {
      var msg = "table is ";
      var left;
      left = document.getEle mentById("table ").offsetLe ft;
      msg = msg + left;

      msg = msg + " and ";

      var Element = document.getEle mentById("table ");
      var CalculatedTotal OffsetLeft;
      CalculatedTotal OffsetLeft = 0;
      while (Element.offset Parent) {
      CalculatedTotal OffsetLeft += Element.offsetL eft;
      Element = Element.offsetP arent;
      }
      msg = msg + CalculatedTotal OffsetLeft;
      msg = msg + " px away from left of screen.";
      alert(msg);
      }
      </script>
      <br>
      <br>
      </body>
      </html>
      --------------------------------------------------


      "Martin Honnen" <mahotrash@yaho o.de> wrote in message
      news:4120a423@o laf.komtel.net. ..[color=blue]
      >
      >
      > Keith Thornhill wrote:
      >[color=green]
      > > I'm trying to calculate the top and left distance from the side of the
      > > browser of an object (either absolutely or relatively positioned) and[/color][/color]
      i'm[color=blue][color=green]
      > > running into a weird problem as i'm trying to make the code[/color][/color]
      cross-browser.[color=blue][color=green]
      > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      > > "http://www.w3.org/TR/html4/loose.dtd">
      > > <html>
      > > <head>
      > > <meta http-equiv="Content-Type" content="text/html;
      > > charset=iso-8859-1">
      > > <title>Untitl ed Document</title>
      > > </head>
      > >
      > > <body>
      > > <table align="center" id="table" border="1">
      > > <tr>
      > > <td>test</td>
      > > </tr>
      > > </table>
      > > <br>
      > > <br>
      > > table is
      > > <script>
      > > var left;
      > > left = document.getEle mentById("table ").offsetLe ft;
      > > document.write( left);
      > >
      > > document.write( " and ");
      > >
      > > var Element = document.getEle mentById("table ");
      > > var CalculatedTotal OffsetLeft;
      > > CalculatedTotal OffsetLeft = 0;
      > > while (Element.offset Parent) {
      > > CalculatedTotal OffsetLeft += Element.offsetL eft ;
      > > Element = Element.offsetP arent ;
      > > }
      > > document.write( CalculatedTotal OffsetLeft);
      > > </script>[/color]
      >
      > What happens if you wait for the load event to fire and then check the
      > offset values in window.onload (not using document.write obviously)?
      > Does that give you the values you are looking for?
      >
      > --
      >
      > Martin Honnen
      > http://JavaScript.FAQTs.com/
      >[/color]


      Comment

      • Alberto

        #4
        Re: offsetLeft/Top Bug in Firefox? (value is 8)

        I don't have firefox - it meddles with my netscape 7, dunno why...

        Maybe worth a try changing
        Element.offsetP arent
        with
        Element.parentN ode

        lemme know if that does anything,
        ciao
        Alberto


        "Keith Thornhill" <pipthedog@cox. net> ha scritto nel messaggio
        news:tuQTc.2054 9$Yf6.17699@lak eread03...[color=blue]
        > I'm trying to calculate the top and left distance from the side of the
        > browser of an object (either absolutely or relatively positioned) and i'm
        > running into a weird problem as i'm trying to make the code cross-browser.
        >
        > normally i simply use the .offsetLeft/Top properties of the object to get[/color]
        my[color=blue]
        > numbers, and it is as simple as that. but in Firefox (v. 0.9.3), they[/color]
        both[color=blue]
        > return the value "8" no matter where the object appears on the page.
        >
        > i've tried many ways to make this work, and always with the same results.
        >
        > my current proof of concept page (included below) contains two methods[/color]
        (one[color=blue]
        > direct and one recursive) which both work in IE (v. 6) and Opera (v. 7.54)
        >
        > any help to get this working in Firefox would be greatly appreciated.
        >
        > thanks,
        > keith
        >
        > ----------------------------------------------------
        > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        > "http://www.w3.org/TR/html4/loose.dtd">
        > <html>
        > <head>
        > <meta http-equiv="Content-Type" content="text/html;
        > charset=iso-8859-1">
        > <title>Untitl ed Document</title>
        > </head>
        >
        > <body>
        > <table align="center" id="table" border="1">
        > <tr>
        > <td>test</td>
        > </tr>
        > </table>
        > <br>
        > <br>
        > table is
        > <script>
        > var left;
        > left = document.getEle mentById("table ").offsetLe ft;
        > document.write( left);
        >
        > document.write( " and ");
        >
        > var Element = document.getEle mentById("table ");
        > var CalculatedTotal OffsetLeft;
        > CalculatedTotal OffsetLeft = 0;
        > while (Element.offset Parent) {
        > CalculatedTotal OffsetLeft += Element.offsetL eft ;
        > Element = Element.offsetP arent ;
        > }
        > document.write( CalculatedTotal OffsetLeft);
        > </script>
        > px away from left of screen.<br>
        > <br>
        > </body>
        > </html>
        > ----------------------------------------------------
        >
        >[/color]


        Comment

        • Grant Wagner

          #5
          Re: offsetLeft/Top Bug in Firefox? (value is 8)

          Keith Thornhill wrote:
          [color=blue]
          > When using the following code, I get the same results.
          >
          > --------------------------------------------------
          > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          > "http://www.w3.org/TR/html4/loose.dtd">
          > <html>
          > <head>
          > <meta http-equiv="Content-Type" content="text/html;
          > charset=iso-8859-1">
          > <title>Untitl ed Document</title>
          > </head>
          >
          > <body onLoad="check() ">
          > <table align="center" id="table" border="1">
          > <tr>
          > <td>test</td>
          > </tr>
          > </table>
          > <br>
          > <br>
          > <script>
          > function check() {
          > var msg = "table is ";
          > var left;
          > left = document.getEle mentById("table ").offsetLe ft;
          > msg = msg + left;
          >
          > msg = msg + " and ";
          >
          > var Element = document.getEle mentById("table ");
          > var CalculatedTotal OffsetLeft;
          > CalculatedTotal OffsetLeft = 0;
          > while (Element.offset Parent) {
          > CalculatedTotal OffsetLeft += Element.offsetL eft;
          > Element = Element.offsetP arent;
          > }
          > msg = msg + CalculatedTotal OffsetLeft;
          > msg = msg + " px away from left of screen.";
          > alert(msg);
          > }
          > </script>
          > <br>
          > <br>
          > </body>
          > </html>[/color]

          The problem appears to be specifically related to ALIGN="CENTER" on the
          <TABLE>. If I set ALIGN="RIGHT", it reports the correct results. I managed to
          resolve the problem by doing the following:

          <div align="center"> <table id="table" border="1">...</table></div>

          Your code remains the same, but Firefox 0.9.3 now reports the correct results
          (as do IE and Opera).

          It may not be the solution you want, but it appears to be the only one that
          works in Mozilla 1.7.2 and Firefox 0.9.3.

          You may want to check bugzilla for this problem, and if you can't find it
          reported, open a new bug report describing the problem and provide a
          proof-of-behaviour page so it can be fixed for the next release.

          --
          Grant Wagner <gwagner@agrico reunited.com>
          comp.lang.javas cript FAQ - http://jibbering.com/faq

          Comment

          Working...