§ 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.
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
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
- Walid on "Adventures with Ubuntu 9.10 on G4 Yikes!" - I am in the same process as you. Although I still dual-boot to Windows for my ganimg leisure. My first contact with Ubuntu was way…
- Slashback on "Simpler adding & removing element classnames" - Actually, the addClassName function does what you want, pretty much the same way you suggest.
- angie on "Simpler adding & removing element classnames" - And what if I want to add a class, but keeping the csseals the element already has?I mean, I have an element with a class, let's name…
- backlinks on "Adjust iframe height to contents" - Definitely helped me.
- Slashback on "Simpler validate form fields are filled in (no additional class names)" - You are welcome!
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