The Netlobo logo - a Nevada desert landscape

Comments for Managing Multiple Javascript window.onload Functions

< Back to the article

27 comments for this article.

RSS Feed Icon Subscribe to the comments for this article

Posted: 2007-08-10 23:09:59 by Deb
I needed this exact fix and so far, on IE7 and FF2 on PC, it seems to work. Thanks!
Posted: 2007-08-27 03:31:43 by Mike
This makes a world of difference when including variable components into an application! Thank you :)
Posted: 2007-09-04 16:42:17 by Warwick
How does using onload=" ... " affect this code?
Posted: 2007-09-04 16:50:46 by lobo235 - Netlobo Staff Member
Using onload="..." in conjunction with this code can have two side effects. If you set an onload function manually and then call the womOn function it will overwrite your onload function and it will be forgotten. If you call womOn and then manually set the onload then the WOM will not work as it will have been overwritten by your manual onload.

I believe that any body.onload functions will work in conjunction with the WOM as the WOM uses window.onload.
Posted: 2008-01-31 13:21:48 by nomikos
Thanks lobo235!
BTW Sometimes not all functions are available on each page. Then:

for (var i = 0;i < woms.length;i++)
if (typeof woms[i] == 'function')
eval(woms[i]);

Right?
Posted: 2008-01-31 13:24:57 by lobo235 - Netlobo Staff Member
nomikos,

That will work for many situations where the value in woms[i] is just the name of a function. However, if you have something like 'setFocus("username");' stored in woms[i] then the typeof() call will not return 'function'. You should be careful with this.
Posted: 2008-07-17 12:06:24 by rook2pawn
Hey lobo235,

btw, the best way to do this is
document.addEventListener("DOMContentLoaded", load1_func, false);
document.addEventListener("DOMContentLoaded", load2_func, false);
document.addEventListener("DOMContentLoaded", load3_func, false);

etc... You can sprinkle them anywhere you like across as many files you want. No overhead. Except you have to handle IE ina special way!

Just a thought!
Posted: 2008-07-17 12:11:40 by lobo235 - Netlobo Staff Member
rook2pawn,

Like you said, you have to handle IE as a special case if you do it that way because IE doesn't have the document.addEventListener() function. IE only supports the document.attachEvent() function. This makes your solution more complicated because you have to determine which browser the user is using and call the right function based off of that.

Because of that problem (and others) I find it easiest to use the WOM. Afterall, that's the reason I wrote it.
Posted: 2008-08-18 08:02:48 by kruncher
Hey!

That is a fantastic way to deal with multiple handlers. I think that I can put this to use for other events as well as the onload!

Out of curiosity, is there any difference between the following two code snippets?

Snippet #1
<script type="text/javascript">
     window.onload = function() {
         alert('Just a quick test');
     }
</script>



Snippet #2
<script type="text/javascript">
     alert('Just a quick test');
</script>


If these are identical, is it even necessary to use the window.onload event handler? Or are there any added benefits of this event?
Posted: 2008-08-18 08:05:45 by lobo235 - Netlobo Staff Member
kruncher,

The difference between doing the alert at the window.onload event is that the alert will occur once the page has finished loading. If you don't do it in the window.onload event then the alert will happen as soon as the browser parses the alert line in your html document.

There are many javascript functions that depend on a certain element existing in your page. To ensure that the element has been parsed by the browser and is visible to the user it is necessary to perform the function after the window has loaded fully and that is why you set the function to run on the window.onload event.

Hope this helps,
lobo235
Posted: 2008-08-27 08:22:34 by jalil
first of all many thanks for a great solution.

i am developing some apps in jooma and used your wiggly WOM for some of the 3rd party menus and javascripts i adopted.

here are some tips for other users, who may encounter the need to make some small modifications, in my case i had to as my app is large and i use womAdd all ove the place, some of them may not be required at certian pages which will lead to JS error warnings, which is not good.

so to supresss these warnings, i made a slight modification to womGo and womAdd, this works well for me all all the 3rd party js runs green.


/*************************************************************
* The womGo() function loops through the woms array and
* runs each function in the array.
*************************************************************/
function womGo(){
for(var i = 0;i < woms.length;i++)
if (woms[i]!='undefined') eval(woms[i]);
}
/*************************************************************
* The womAdd() function will add another function to the woms
* array to be run when the page loads.
*************************************************************/
function womAdd(func){
if (func !='undefined') woms[woms.length] = func;
}
/*************************************************************


there are other precautions i took even *before* calling wom, this helps as a double protection against calling inappropriate onload events.

again, many thanks for your excellent work, all credits *and* alink to this page is inserted in the credits column of the upcoming version.

cheers.

Posted: 2008-09-03 08:20:34 by AMPwerx
lobo235

I've NeVeR coded javascript before but after spending a few hours at netlobo I feel confident that my client's site is as they wished it to be.

It made an untidy group of .flv files look down-right professional. I'm still not a 100% sure if all is correct, but it validates and appears to be doing the trick. You can take a look by searching the site for ....seniors_music_outreach_initiative...

Thanks for saving me time, money, aggrevation and bewilderment.

Your articles: Showing and Hiding a DIV using CSS and Javascript; Showing/Hiding a DIV using the Off-Left Method; and WOM have saved me weeks of wading through an ocean of code-sloggers.

A blog entry and link will be forthcoming when something called 'time' comes my way again.

-A

Posted: 2008-09-25 09:06:48 by eniauq
Thank you!
I took your idea and used it with
document.addEventListener("load","initFunctions",false) where initFunction calls womGo().
The onload initialisation is cleaner and thanks to you, the order of the functions is guaranteed. It is also easy to set their parameters.
Posted: 2008-12-04 09:48:59 by MPower
I don't think I've done right.
At the index page I've put the following in top:

<script type="text/javascript" src="inc/WOM.js"></script>


On my menu page that's allways includes I've put the following codes in the top:

<script type="text/javascript" src="inc/menu_tree.js"></script>
<script type="text/javascript">
	womAdd('MinitTree()');
	womOn();
</script>


menu_tree.js includes the function MinitTree().

On another page that's not allways loaded I done the same for the JS files for that page. For the womAdd in this page it looks like this:

womAdd('MinitTree()');
Posted: 2008-12-04 10:02:29 by lobo235 - Netlobo Staff Member
MPower,

Send us a message using our contact page and we can take a look at your page to see if we can figure out what is going on.

Thanks,
lobo235
Posted: 2008-12-24 10:19:06 by cefleet
Greetings,
Im a PHP programmer and i use the cake php framework. I just wanted to let you know this is exactly what i needed for my designer to be able to add JS with out messing with my code. Thank you very much.
Posted: 2009-04-13 14:22:12 by Cecilia
Thanks for your article. I have a question:

What's the difference between WOM and body.onload?
For example:
<script type="text/javascript">
womAdd('func1');
womAdd('func2');
womAdd('func3');
womOn();
</script>

<body onLoad="func1(); func2();func3();">

Thanks,
Cecilia
Posted: 2009-04-13 14:24:14 by lobo235 - Netlobo Staff Member
Cecilia,

Functionally they are equivalent. For large applications where many window.onload functions may be required the WOM allows you to have as many window.onload functions as you need without worrying about overwriting other window.onload functions. Of course, all the window.onload functions need to be done using WOM for it to be beneficial.
Posted: 2009-04-20 10:49:59 by jerrydev
I am a newbie to Java scripting.....
I am developing a website, and i have 3 load events. When i use your script, i am able to load only 2 of those events...My 3rd always does not load. Check the web page at
http://ez-host2.com/tp40/page.asp?ID=194266#

Any help will be appreciated.

Thanks,
Jerry
Posted: 2009-05-12 14:36:46 by alasdair
Great script and just what I needed. For anyone looking to load multiple scriptalicious effects WOM works wonderfully.

Many thanks for sharing this.
Posted: 2009-06-20 14:59:57 by OP351
Tested in IE8, FF3 and Opera9. Runs fine. Good job. Thanks
Posted: 2009-06-26 14:18:20 by libanez
Hi there, great idea and piece of code here!

I'm just wondering under which license you are distributing it, i want to use it in a GPL code and i need to know precisely the terms of use.

Seems lgpl to me, but its your decision.

Thanks in advance!
Posted: 2009-06-26 14:19:26 by lobo235 - Netlobo Staff Member
LGPL sounds good. It's free for anyone to use all I ask is that you consider putting the URL of the page in the comments of the source.
Posted: 2009-06-26 16:22:02 by libanez
Great, but then you should mention it in the header.
This way everybody knows the terms, even if it founds the code at other source (i did it that way) . I'll be waiting for that to copy the exact notice in my code.

Thanks again.
Posted: 2009-07-25 08:34:46 by Ashley
Hi, this may sound like a stupid question, but I know nothing about scripting, all I know is HTML and CSS.

My question is, what do you do once you have created the WOM.js file and linked it to the html document that has multiple Javascript documents that conflict?
Posted: 2009-12-17 09:24:09 by Yair
Thanks a lot !!
Posted: 2010-01-28 01:02:25 by astembridge
Many thanks for this simple, elegant workaround for complex onload environments.

RSS Feed Icon Subscribe to the comments for this article

Post your comment for the Managing Multiple Javascript window.onload Functions article:

Name (required) (letters and numbers only):
Email (required) (will not be published):
Website (include http://):
Comment (required): (HTML tags allowed: pre, strong, em, b, i)