[thelist] [JS] window.onload is a liar...

Tom Dell'Aringa pixelmech at yahoo.com
Tue Mar 16 08:12:51 CST 2004


--- liorean <liorean at f2o.org> wrote:
> Note that when you assign the function y, it is not executed, and
> thus y does not call x. If you on the other hand call y, y will be
> executed and in turn call x, which will be executed.

Great - so the real difference is time of execution of the function.
When I do:

window.onload = a;

then a runs when the onload event fires.

If however I do 

window.onload = a();

The return value is immediately assigned because a is executed - and
since the page isn't loaded yet - it will most likely be undefined.

That makes sense. So when I do:

window.onload = function() {
  a();
}

the anonymous function is NOT executed immediately. So this is the
same as assigning a function (onload = a;) it fires when the event
fires. The anon function does not evaluate and return like a named
function would. Have I understood you correctly?

Thanks for taking the time. Hoping more people than just me are
finding this helpful :)

Tom




=====
http://www.Pixelmech.com/ - read my latest blog posting!
http://www.DMXzone.com/ - JavaScript Author
http://SparklesParties.com - Princess parties for little girls!
http://www.thywordistruth.net/ - Eternal Life

"Well, my name's Dewey Oxburger. My friends call me Ox. I dont know if you've noticed, but I got a slight weight problem."





More information about the thelist mailing list