[Javascript] looping through nodeList
Paul Novitski
paul at novitskisoftware.com
Tue Oct 18 16:56:44 CDT 2005
At 02:15 PM 10/18/2005, Anthony Ettinger wrote:
>How should I call function names then?
>
>If I run window.onload=Foo; it works,
>
>if I have:
>
>window.onload=Bar;
>
>function Bar()
>{
> Foo;
>}
>
>Foo does not get called. It only works in the 2nd
>instance if I call it as Foo();
>
>What's going on here?
To reiterate Mike's point:
_____________________________
If your javascript statement is:
Foo;
then you are invoking the TEXT of the function Foo(). This is like writing:
"funky";
It doesn't do anything, it just sits there. It's just an expression
with no verb.
_____________________________
If you state:
Foo();
then you are executing the function Foo(). In this case, the
browser's javascript interpreter performs the logic of the Foo() function
_____________________________
The reason we generally write:
window.onload = Foo;
and not:
window.onload = Foo();
is that we want window.onload to equal the TEXT of function Foo();
when the page finishes loading, the function text contained in
window.onload will execute. It's an exact copy of your function
Foo() and will perform identically.
_____________________________
Rarely will you ever want to write:
window.onload = Foo();
This would make sense only if function Foo() returned javscript code,
something like this:
window.onload = Foo();
function Foo()
{
return("alert('hello world')");
}
window.onload would then contain the script:
alert('hello world')
which would execute when the page finished loading.
Clear as mud?
Paul
More information about the Javascript
mailing list