Add multiple window.onload events

64,032

Solution 1

Most of the "solutions" suggested are Microsoft-specific, or require bloated libraries. Here's one good way. This works with W3C-compliant browsers and with Microsoft IE.

if (window.addEventListener) // W3C standard
{
  window.addEventListener('load', myFunction, false); // NB **not** 'onload'
} 
else if (window.attachEvent) // Microsoft
{
  window.attachEvent('onload', myFunction);
}

Solution 2

There still is an ugly solution (which is far inferior to using a framework or addEventListener/attachEvent) that is to save the current onload event:

function addOnLoad(fn)
{ 
   var old = window.onload;
   window.onload = function()
   {
       old();
       fn();
   };
}

addOnLoad(function()
{
   // your code here
});
addOnLoad(function()
{
   // your code here
});
addOnLoad(function()
{
   // your code here
});

Note that frameworks like jQuery will provide a way to execute code when the DOM is ready and not when the page loads.

DOM being ready means that your HTML has loaded but not external components like images or stylesheets, allowing you to be called long before the load event fires.

Solution 3

I had a similar problem today so I solved it having an index.js with the following:

window.onloadFuncs = [];

window.onload = function()
{
 for(var i in this.onloadFuncs)
 {
  this.onloadFuncs[i]();
 }
}

and in additional js files that i want to attach the onload event I just have to do this:

window.onloadFuncs.push(function(){
 // code here
});

I normally use jQuery though, but this time I was restricted to pure js wich forced to use my mind for a while!

Solution 4

Mootools is another great JavaScript framework which is fairly easy to use, and like RedWolves said with jQuery you can can just keep chucking as many handlers as you want.

For every *.js file I include I just wrap the code in a function.

window.addEvent('domready', function(){
    alert('Just put all your code here');
});

And there are also advantages of using domready instead of onload

Solution 5

Try this:

window.attachEvent("onload", myOtherFunctionToCall);

function myOtherFunctionToCall() {
    // do something
}

edit: hey, I was just getting ready to log in with Firefox and reformat this myself! Still doesn't seem to format code for me with IE7.

Share:
64,032
Ray
Author by

Ray

Updated on November 17, 2020

Comments

  • Ray
    Ray over 3 years

    In my ASP.NET User Control I'm adding some JavaScript to the window.onload event:

    if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), onloadScriptName))
      Page.ClientScript.RegisterStartupScript(this.GetType(), onloadScriptName, 
        "window.onload = function() {myFunction();};", true);            
    

    My problem is, if there is already something in the onload event, than this overwrites it. How would I go about allowing two user controls to each execute JavaScript in the onload event?

    Edit: Thanks for the info on third party libraries. I'll keep them in mind.

  • Ray
    Ray over 15 years
    Don't these scripts get executed when the page gets to them? Not during onload?
  • user961954
    user961954 over 12 years
    Note: function assigned(myFunction) in window.attachEvent('onload', myFunction); is always fired after any function assigned to window.onload
  • meJustAndrew
    meJustAndrew over 7 years
    excuse me sir, what does NB not 'onload' means?
  • fmacdee
    fmacdee about 7 years
    @meJustAndrew he means that when you use addEventListener you don't use 'onload' you just use 'load'.