Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] looping through nodeList
- From: paul at novitskisoftware.com (Paul Novitski)
- Subject: [Javascript] looping through nodeList
- Date: Tue Oct 18 16:56:53 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
- Follow-Ups:
- [Javascript] looping through nodeList
- From: Anthony Ettinger
- [Javascript] looping through nodeList
- References:
- [Javascript] looping through nodeList
- From: Mike Dougherty
- [Javascript] looping through nodeList
- From: Anthony Ettinger
- [Javascript] looping through nodeList
- Prev by Date: [Javascript] looping through nodeList
- Next by Date: [Javascript] looping through nodeList
- Previous by thread: [Javascript] looping through nodeList
- Next by thread: [Javascript] looping through nodeList
- Index(es):