Multiple On Load Events
A common issue that arises when developing web applications with JavaScript is that the onload events interfere with each other. With all the freely available frameworks and apps at a users disposal there is a strong possibility the onload event is already being used in a project… incorrectly. (I admit I am guilty of this.)
Rather than overwriting the previous call by hoping your code appears after it. You can simply append your function to the event with a addListener function. As usual IE needs special attention.
function firstFunction(){ alert("1st Function"); } function secondFunction(){ alert("2nd Function"); } window.onload = firstFunction; if(typeof window.addEventListener == "function"){ window.addEventListener("load", secondFunction, false); } //IE else if(typeof window.attachEvent == "object"){ window.attachEvent("onload", secondFunction); }
If your code appears before the original onload function your function will be executed first.
Todo List
- 06/30/2009
- Degrade for older browsers.











