Monday, 20 August 2007

A compact Javascript multi onload function

Theres plenty of other javascript snippets to allow multiple 'onload' events on the net, but probably none as small as this.

Heres a little example with the magic function

Enjoy!

<script>
function Multi(f){var o=window.onload;window.onload=function(){o?o():o;f()}}

//the old way ;)
window.onload = function() {alert("onload!");}
//example :)
new Multi(function() {alert('second');});
new Multi(function() {alert('three');});
</script>

1 comment:

Ed said...

Hi Tad,

I have a few Javascripts on a web page and I encountered the "window.onload".

http://blog.roberthahn.ca/articles/2007/02/02/how-to-use-window-onload-the-right-way

Unfortunately, I don't have any knowledge whatsoever regarding Javascript, and although I did my best(...), I could not implement this solution.

If I send you the two (very short) scripts, will you please tell me how/where should I use the additional code?

Thank you,

Ed
www.just-looking.net