Colin,
My JavaScript is rusty, so someone will come along and correct me, but maybe I'll get one of these right, and it will save them typing. Here are my understandings:
1. difference between a <body onload...> event handler, and code in a script tag that's not part of a function:
the onload event fires after the page has fully loaded.... while the "outside a function" script can run as soon as it has loaded (doesn't wait for the rest of the document). So, if the script depends on the page loading (for example, it references some form element to give it focus), it should be in the body tag's onload event handler.
2. How does control ever get back from the call to "showTimer()" in your example?
The call to setTimeout() is asynchronous - meaning that the control flow of your script continues, after starting a new
thread - in that new thread, after 1000 ms, the function will be called again, run straight through to completion (including kicking off another new separate thread to run the function again), and finish normally.
It sounded like you were thinking of this as a recursive call - where the same thread would call the function again before finishing the first run of the function, and wait for that second call to complete before continuing with the first... but then, that second invocation of the method would call it a third time, which would call it a fourth time.... and each function call wouldn't finish until the call
it made returned.... which would be never.
If you had the call in showTimer() directly inside showTimer() (and not by way of setTimeout()), that's exactly what you would have. You also would have a stack overflow, at some point... there's only finite memory set aside for the call stack, and you would eentually run out.
3. When do we need to use "javascript:", and when can we just give the name of the method?
I dunno - I don't think you need the "javascript:" before the method call in your second case. The "onnClick=" is already an event handler...
The only place I can remember using "javascript:" like that was in the HREF of a link... it went something like this:
Hope this helps,
-- Jon
[ February 18, 2006: Message edited by: Jon Egan ]