Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > window.onload and body.onload differences

Reply
Thread Tools

window.onload and body.onload differences

 
 
David Otton
Guest
Posts: n/a
 
      11-04-2004
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>
 
Reply With Quote
 
 
 
 
Cenekemoi
Guest
Posts: n/a
 
      11-04-2004
Bonjour à David Otton <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-04-2004


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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-14-2009 03:51 PM
DEVELOP THE WINNING EDGE, SMALL DIFFERENCES IN YOUR PERFORMANCE CANLEAD TO LARGE DIFFERENCES IN YOUR RESULTS Home_Job_opportunity C Programming 0 01-08-2009 04:31 PM
Differences between getImage and createImage and using Toolkit Thomas Fritsch Java 2 07-27-2006 06:33 AM
J2EE and .NET, similarities, differences and interoperability Mohammad Java 0 02-21-2006 07:38 PM
XML-RPC and SOAP:differences and general issues... RedHat_79 XML 0 05-29-2004 11:21 AM



Advertisments