This is an update to a previous article written way back in 2003 "Managing multiple onLoad events in JavaScript".
The article describes how to create an array of onload (window.onload) events in order to queue each event and enable the browser to perform onload sequentially. However this can all be forgotten in preference to the Prototype Javascript Framework and the useful Event Observe method.
Use Event.observe instead of using <body onload="return myFunction()"> or DOM Level-0 event property window.onload = myFunction;
Example
-
Event.observe(window, 'load', function() { myFunction.init() }); -
Event.observe(window, 'load', function() { myFunction2.init() });
- Comments (2) Back home
- Filed under:
- Scripting
- JavaScript
- Prototype
- Posted by:
- James Griffin
- Dated:
- June 16, 2007
2 comments
Sadly, this alone does not guarantee that the events will fire in the order you specify. I'm wrestling with this right now.
As these events are all set to fire when the window object is loaded, they all fire concurrently once the condition is satisfied, and it's left to the browser how to prioritize them.
Can the multiple onload function at AskApaches - favorite javascript '07 be used?