How To Use Multiple Onload Actions on the Same Page
Web applications like FileChucker and UserBase often need to perform tasks that happen after a web page has finished loading. The traditional way to do this is to add onload="somefunc();" to the page's <body> tag. But what if you need multiple events to happen at onload time?
To schedule multiple onload events, add the following code to the <head> section of your page:
<script type="text/javascript">
function schedule_onload_action(newfunc)
{
var already_scheduled = window.onload;
if(typeof window.onload != 'function')
{
window.onload = newfunc;
}
else
{
window.onload = function()
{
already_scheduled();
newfunc();
}
}
}
</script>
Then take the code that was in your onload="" attribute and add it to a new Javascript function. For example, if you previously had this:
<body onload="somefunc('foo', 'bar', 'baz')">
...then you'd replace that with simply "<body>", and create the following new Javascript code in your page's <head> section:
<script type="text/javascript">
function do_somefunc()
{
somefunc('foo', 'bar', 'baz');
}
schedule_onload_action(do_somefunc);
</script>
And of course, the whole point is that you can then include as many calls to the schedule_onload_action() function as you wish:
<script type="text/javascript"> schedule_onload_action(do_somefunc); schedule_onload_action(do_something_else); schedule_onload_action(do_that_other_thing); </script>
