0

Multiple On Load Events

Multiple On Load Events via @largestartist Posted by Jose Cuadra on June 30, 2009 Tags: | Filed Under: References

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.

Demo

Download

Todo List

06/30/2009
Degrade for older browsers.

Want More? Try These.

Related Articles
Flash Slideshow in Actionscript 3
Target Blank for XHTML
fliteBox Lightbox for Flickr
The Coolest Lightboxes Around
Random Articles
25 Free Font Sites
Access Cascading Styles with JavaScript
Twitter Highlights of the Week
5 Pointz by Passetti
Posemaniacs Anatomy
Disable Safari’s Textarea Grip
fliteBox Lightbox for Flickr
The Definitive List of Adobe Alternatives
Multiple On Load Events
ActionScript 2 to 3 Object Properties

Leave a Reply