window.onload and body.onload differences

Discussion in 'Javascript' started by David Otton, Nov 4, 2004.

  1. David Otton

    David Otton Guest

    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.getElementsByTagName("*");
    var str = "# of tags: " + allTags.length + "\n";
    alert (str);
    }

    window.onload = countTags();
    </script>
    </head>
    <body onload="countTags();">
    <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="countTags();">Count tags</button></p>
    </body>
    </html>
    David Otton, Nov 4, 2004
    #1
    1. Advertising

  2. David Otton

    Cenekemoi Guest

    Bonjour à David Otton <> qui nous a écrit :
    > <!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.getElementsByTagName("*");
    > var str = "# of tags: " + allTags.length + "\n";
    > alert (str);
    > }
    >
    > window.onload = countTags();


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

    window.onload = countTags;

    > </script>
    > </head>
    > <body onload="countTags();">


    But now, you have a new value for window.onload !...

    > <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="countTags();">Count tags</button></p>
    > </body>
    > </html>



    --
    Cordialement, Thierry ;-)
    Cenekemoi, Nov 4, 2004
    #2
    1. Advertising

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


    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
    http://JavaScript.FAQTs.com/
    Martin Honnen, Nov 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    4
    Views:
    1,594
    Jason
    May 7, 2007
  2. Home_Job_opportunity
    Replies:
    0
    Views:
    483
    Home_Job_opportunity
    Jan 8, 2009
  3. Home_Job_opportunity
    Replies:
    0
    Views:
    568
    Home_Job_opportunity
    Jan 14, 2009
  4. Starry Gordon
    Replies:
    1
    Views:
    105
  5. Replies:
    18
    Views:
    526
Loading...

Share This Page