« Blog Home

§ window.onload via double delegate function

This is the way I add javascript functions to the window.onload event.  As far as I've seen, you can load endless functions this way.

Lifted straight from Robert Hahn's blog:  http://blog.roberthahn.ca/articles/2007/02/02/how-to-use-window-onload-the-right-way

Thank you!  Bless you!  What a headache.

Safe way to add window.onload functions.  It should work for other events as well.

I'm pretty sure I found this on a web page, but (of course) I didn't save the name of the kind soul who wrote it.  If it's yours, or you know who wrote it, please let me know and I'll give due credit.  Ahh, found it:  http://blog.roberthahn.ca/articles/2007/02/02/how-to-use-window-onload-the-right-way from Robert Hahn.

You may note that I use this... well, everywhere.

 

function makeDoubleDelegate(function1, function2) {
  return function() {
    if (function1)
      function1();
    if (function2)
      function2();
  }
}
window.onload = makeDoubleDelegate(window.onload, setParentIframeHeight);

last edited on January 29th, 2009 at 12:07 PM

Comments

Slashback says:

The makeDoubleDelegate() function is rather simple and straightforward, so I find it quite odd that Chrome can't handle it.

I do not have Chrome installed. I tried it in the past but found it very invasive. I found it difficult to uninstall and finally get rid of all of the changes it made to my system. This is totally unacceptable to me, and I consider it irresponsible to create products that behave this way.

However, I just set up a virtual machine just for the purpose of checking this in Chrome. On WinXPSP3, I installed today's Chrome download, which shows version 4.1.249.1045 (42898) in its About box. As far as I can tell, makeDoubleDelegate() works just fine...

April 9th, 2010 at 9:14 PM

Joaquin Serra says:

Hello, i'm using this function one year but i realized now that in Google chrome doesn't work. Have you got some solution?

April 6th, 2010 at 2:58 AM

Slashback says:

The makeDoubleDelegate function just needs to be available, so include it once somewhere in your html file or an included javascript file. For each function you want to run when the window loads,
window.onload = makeDoubleDelegate(window.onload, afunction);
window.onload = makeDoubleDelegate(window.onload, anotherFunction);
Those other functions have to be defined somewhere, of course. The order or placement of all these functions makes no difference, but the order of the lines that call the makeDoubleDelegate function will control the order those various function will run.

February 18th, 2010 at 3:30 PM

Neil Haskins says:

I don't understand exactly how this is to be used; I've just used javascript in a cut and paste manner really. Does the whole code go into 1 script, or does the last line appear at the beggining of each script, with the appropriate function name replacing "setParentIframeHeight"?
Basically what goes where?

February 17th, 2010 at 4:44 PM

Add your comment

Name:
Email: (Will not be displayed - Privacy policy)
Website:
Comments:
  random image I can't read it!
Give me a different one!
Verify Post: Input the text from the image above to verify this post (helps prevent spam)
 

« Blog Home


I don’t hold myself out as a role model generally, but I think the world might be better off if more people accepted responsibility and dealt with consequences.
David Drake, Author's Note to In the Stormy Red Sky