Forum Moderators: open

Message Too Old, No Replies

What is the best way to use onload?

Loading multiple pages using onload.

         

timmah100

12:01 am on Jul 26, 2008 (gmt 0)

10+ Year Member




onLoad="javascript:attach_file('mysql_insert.php');

This works perfect, but I need to load about 9 different pages when a certain page loads. I thought it would be easier to just use an onLoad function in the header, but I keep getting errors by doing this


onLoad="javascript:attach_file('mysql_insert.php'); ('plumbing_insert.php');

Can anybody help me out on how to load multiple pages with the onLoad function?

Thanks in advance

[edited by: DrDoc at 12:39 am (utc) on July 26, 2008]

Fotiman

12:18 am on Jul 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Put this just before your closing </body> tag:

<script type="text/javascript">
window.onload = function() {
attach_file('mysql_insert.php');
attach_file('plumbing_insert.php');
// attach_file('someotherefile.php');
// and so on...
};
</script>

And remove the onLoad event handler from your <body>.

Fotiman

12:22 am on Jul 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, the *best* way to do this would be to "attach" an event listener, so it doesn't matter what anyone else does with the onload event handler. You could do this with most JavaScript frameworks (or write your own). Here is how you could do it with the Yahoo UI Library:


<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/yahoo/yahoo-min.js" ></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/event/event-min.js" ></script>
<script type="text/javascript">
YAHOO.util.Event.on(window, 'load', function() {
attach_file('mysql_insert.php');
attach_file('plumbing_insert.php');
// attach_file('someotherefile.php');
// and so on...
});
</script>