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

liorean liorean at f2o.org
Tue Mar 16 09:13:15 CST 2004


Tom Dell'Aringa wrote:
> the anonymous function is NOT executed immediately.

Correct

> So this is the same as assigning a function (onload = a;) it fires when the event fires.

Yes

> The anon function does not evaluate and return like a named
> function would. Have I understood you correctly?

Not quite, Tom. There's no real difference between anonymous and named 
functions. It will evaluate and return exactly as any other function. 
However, like with the named functions, anonymous functions are values. 
If you want them to evaluate, you need to call them.

Here's a comparison for you:

//Function as value:
function NamedThroughStatement(){/*code*/}

a=function NamedThroughOperator(){/*code*/},
b=function(){/* anonymous function through operator...*/},
c=NamedThroughStatement,
d=(function(){/* anonymous function through operator...*/});

// Function call
e=NamedThroughStatement();
f=a();
g=b();
h=c();
i=(function(){/* anonymous function through operator...*/})();

Marcus Andersson wrote:
> Event handlers can also return values. It can sometimes be useful for 
> event handlers to return values but the only value that is interesting 
> in that case is false. If you return a false value the event bubbling 
> will stop (this should be handled with cancelBubble on the event object 
> these days...)

No, returning false will not cancel the event cascade (bubbling in this 
case). It will just cancel the side effects of the event trigger. Also 
note that this is not entirely consistent across browsers, as evident 
from this thread on CF: 
<http://www.codingforums.com/showthread.php?s=&threadid=35042>

You could also try this small testcase I wrote:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<title>Event cascading test</title>
<script type="text/javascript">
window.onload=function(a,b,c,d,e){
     a=document.getElementById('link');
     b=a.parentNode;
     c=b.parentNode;
     a.onmouseover=function(e){e=e||window.event;alert(e.type);return true;}
     b.onmouseover=function(e){e=e||window.event;alert(e.type);return 
false;}
     c.onmouseover=function(e){e=e||window.event;alert(e.type);return true;}

}
</script>
<ul>
   <li>
     <a id="link" href="#">Test The Cascade!</a>
   </li>
</ul>
-- 
David "liorean" Andersson

ViewStyles, ViewScripts, SwitchStyles and GraphicsInfo bookmarklets:
<http://liorean.web-graphics.com/>
Hangouts:
<http://codingforums.com/> <http://yourmusicforums.com/>


More information about the thelist mailing list