views:

491

answers:

6

This is rather interesting, I think. Consider following code, both the window.onload and body onload="" call the same function. However, the results are different. It appears to me that window.onload has a problem with collections. Here's the code:

<html>
<script type="text/javascript">

 window.onload = getSpanElements();

 function getSpanElements(){
  var collectionBoolean = document.getElementsByTagName("span")?true:false;
  alert(
   "collection exists? " + collectionBoolean + "; number of collection members: " + document.getElementsByTagName("span").length
  );
 }


</script>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 <title>Untitled Document</title>
</head>
<body onload="getSpanElements()">
 <span> test </span>
</body>

As you can see, both report that the collection exists, however window.onload reports that it has no members. Any ideas?

A: 

I think the window object is created before any actual elements are parsed.

pavpanchekha
i would assume so as well, however it is well aware that the collection exists, that is what confuses me.
Andrej Marinic
+6  A: 
window.onload = getSpanElements();

should be

window.onload = getSpanElements;

The code you have calls the getSpanElements function and assigns its return value as the onload event handler.

David Dorward
absolutely right! thanks :)
Andrej Marinic
+1  A: 

You might want to move your window.onload assignment below the getSpanElements declaration:

<html>
<script type="text/javascript">


        function getSpanElements(){
                var collectionBoolean = document.getElementsByTagName("span")?true:false;
                alert(
                        "collection exists? " + collectionBoolean + "; number of collection members: " + document.getElementsByTagName("span").length
                );
        }

        window.onload = getSpanElements;

</script>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Untitled Document</title>
</head>
<body onload="getSpanElements()">
        <span> test </span>
</body>

At the point in your code where you're assigning the window.onload event handler, getSpanElements() has not yet been defined. Also, the line should be

window.onload=getSpanElements;

not

window.onload=getSpanElements();

The function name without parentheses is a reference to the function. With parentheses, it executes the function and the return value is assigned to window.onload.

David Lively
Since getSpanElements is defined with the function statement rather than the function expression, it is subject to hoisting — so it doesn't need appear before the assignment in the code order.
David Dorward
yes, i made a call instead of a reference. thank you!
Andrej Marinic
+7  A: 

You're setting the function wrong:

window.onload = getSpanElements();

should be

window.onload = getSpanElements;

You're setting the onload handler to the return value of getSpanElements() at the moment.

Greg
absolutely right! thanks :)
Andrej Marinic
+1  A: 

You're wrongly doing this:

window.onload = getSpanElements();

which sets the window.onload to the result of the call to the function getSpanElements (undefined).

You should do this instead:

window.onload = getSpanElements;
Seb
actually, the function returns `undefined`, not `null`
Christoph
Edited now, thanks Christoph :)
Seb
A: 

You have to assign a reference to the function getSpanElements to window.onload - currently, the function doesn't get executed onload, but immediately after parsing.

What you actually assign is the undefined return value.

In short: drop the ().

Christoph