window.onload and body.onload differences

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Otton

    window.onload and body.onload differences

    Hi, I'm seeing a difference in behaviour between

    window.onload = f();

    and

    <body onload="f();">

    Specifically, window.onload appears to fire before all the elements of
    the page have been rendered. As the difference is consistent across
    IE/Moz/Opera, I'm assuming it's deliberate - can anyone point me
    towards where this behaviour of window.onload is defined in the
    documentation? TIA.

    Example code follows:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <title>Test Page</title>

    <script type="text/javascript">
    function countTags(){
    var allTags = document.getEle mentsByTagName( "*");
    var str = "# of tags: " + allTags.length + "\n";
    alert (str);
    }

    window.onload = countTags();
    </script>
    </head>
    <body onload="countTa gs();">
    <p>This is some dummy text</p>
    <p>The quick brown fox jumps over the lazy dog</p>
    <p>One Two Three Four Five</p>

    <p><button onclick="countT ags();">Count tags</button></p>
    </body>
    </html>
  • Cenekemoi

    #2
    Re: window.onload and body.onload differences

    Bonjour à David Otton <djotto@gmail.c om> qui nous a écrit :[color=blue]
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    > <html>
    > <head>
    > <title>Test Page</title>
    >
    > <script type="text/javascript">
    > function countTags(){
    > var allTags = document.getEle mentsByTagName( "*");
    > var str = "# of tags: " + allTags.length + "\n";
    > alert (str);
    > }
    >
    > window.onload = countTags();[/color]

    Be carefull, here "countTags( );" is executed (result : 4).
    You must write :

    window.onload = countTags;
    [color=blue]
    > </script>
    > </head>
    > <body onload="countTa gs();">[/color]

    But now, you have a new value for window.onload !...
    [color=blue]
    > <p>This is some dummy text</p>
    > <p>The quick brown fox jumps over the lazy dog</p>
    > <p>One Two Three Four Five</p>
    >
    > <p><button onclick="countT ags();">Count tags</button></p>
    > </body>
    > </html>[/color]


    --
    Cordialement, Thierry ;-)

    Comment

    • Martin Honnen

      #3
      Re: window.onload and body.onload differences



      David Otton wrote:[color=blue]
      > Hi, I'm seeing a difference in behaviour between
      >
      > window.onload = f();
      >
      > and
      >
      > <body onload="f();">[/color]

      Well there is a difference between HTML markup with embedded JavaScript
      code and pure JavaScript, if you want to use script to assign the onload
      handler then use
      window.onload = f;
      that is assign a function and not the result of a function call (f()).

      --

      Martin Honnen

      Comment

      Working...