window.onload; = func() v/s = func

M

mk834tt

What's happening here?

I have a javascript function funcx.

It runs whether or not I set the window.onload event to "funcx" or
"funcx()" However, with the later construct the DOM method
document.getElementsByTagName("thisKind"), within funcx, returns with
zero elements.

firefox 2...

Thanks.
 
J

Joost Diepenmaat

What's happening here?

I have a javascript function funcx.

It runs whether or not I set the window.onload event to "funcx" or
"funcx()" However, with the later construct the DOM method
document.getElementsByTagName("thisKind"), within funcx, returns with
zero elements.

Do you mean:

window.onload=funcx;

That sets window.onload to the value of funcx, which is a function.

window.onload=funcx();

This sets window.onload to the return value of funcx() (the function
*call*), which might be anything. it will run funcx() immediately, in
all likelyhood before the document is fully loaded.
 
M

mk834tt

Show us some code.

Here's an example. The Joost post seems to explain it; the f()
version runs before the page completely loads. Don't know why, but I
accept it.

<head>
<script type="text/javascript">
function countp() {
var paras = document.getElementsByTagName("p");
alert("Para count: " + paras.length);
}
// window.onload = countp; // runs... count correct
window.onload = countp(); // runs... count 0
...
 
A

Aditya

Here's an example. The Joost post seems to explain it; the f()
version runs before the page completely loads. Don't know why, but I
accept it.

<head>
<script type="text/javascript">
function countp() {
var paras = document.getElementsByTagName("p");
alert("Para count: " + paras.length);
}
// window.onload = countp; // runs... count correct
window.onload = countp(); // runs... count 0
...

hey dude when the window has completely loaded it calls the function
window.onload

so when u say
window.onload = countp
window.onload holds a reference to the countp function

but when u say
window.onload = countp()
window.onload holds reference to the value returned by calling the
function countp

i hope it helped
 
A

Aditya

hey dude when the window has completely loaded it calls the function
window.onload

so when u say
window.onload = countp
window.onload holds a reference to the countp function

but when u say
window.onload = countp()
window.onload holds reference to the value returned by calling the
function countp

i hope it helped

basically what i meant was that the countp() actually calls the
function...before the document is completely loaded
 
T

Thomas 'PointedEars' Lahn

Show us some code.

Here's an example. The Joost post seems to explain it; the f()
version runs before the page completely loads. Don't know why, but I
accept it.

[...]
window.onload = countp(); // runs... count 0

If you seriously don't know why countp() executes before the `load' event
occurs, I would strongly recommend you get some basic programming knowledge
first. `()' is the (empty) argument list of a *function call*. Of course
that is evaluated (and the function is called) when the assignment
expression is evaluated, and before the property change takes place; so well
before the `load' event occurs.

window.onload = countp;

on the other hand does not call anything here, only later when the `load'
event occurs. However,

<body onload="if (typeof event != "undefined") countp(event);">

is safer.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top