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
Web Fonts Suck!
Reiq’s Corel Painter Tutorial
Classy Glassy Orbs
From Work Path to Shape Layer
Target Blank for XHTML
CFCACHE, IE, CSS and XHTML
Pencil Painting with Nickelsen
Twitter Highlights of the Week
Disable Safari’s Textarea Grip
Posemaniacs Anatomy

Leave a Reply