Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-12-2013, 06:54 PM   PM User | #1
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question multiple onload() functions for initialization purposes

Concept question ahead ... RESOLVED

If I create an anonymous function that contains an "onload()" to activate,
will a second anonymous function with a different "onload()" command execute?

Normally I would initialize using one "onload()" function near the end of the <body> tags
Does this rule still apply about having only ONE "onload()" function per program
or does it change if setting-up multiple anonymous functions?

And, if only one "onload()" function is allowed per program,
where is it placed to activate/initialize multiple anonymous functions?

An example of three anonymous function needs (each has its own initialization sequence) would be:
1. 1st function to control menu display.
2. 2nd function to control gallery display.
3. 3rd function to control calendar date picker

Each currently has its own "onload()" initialization sequence.
Would I call them as is, in order of need or remove the "onload()" sections to be placed in one common section?

Last edited by jmrker; 03-13-2013 at 02:39 PM..
jmrker is offline   Reply With Quote
Old 03-12-2013, 07:07 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
If you put the JavaScript at the bottom of the page then you probably will not need the onloads at all. If you do need both then you'll need to add them as listeners as event handlers overwrite one another.

The only situation I have found where onload is actually needed is to test if all the images in the page loaded successfully, for anything else simply run the script at the bottom of the page without waiting for anything else to load.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-12-2013, 07:59 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yeah, I use var imageN = new Image(); imageN.src="..."; imageN.onload=countLoads; to count whether all images are lazy-loaded so I can then start an animation using them, but other than that...

Surely you aren't talking about window.onload(), are you? As Felgall says, if your code is just before the </body> then there's surely no reason to use that.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-12-2013, 09:32 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
there is only one onload property; last one to set it before it fires "wins".

use setTimeout or document.addEventListener("load", function(){...}); instead

you can also do something like

Code:
var oldLoad=window.onload;
window.onload=function(){
  oldLoad();
  ...
}
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 03-13-2013, 03:51 AM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Thumbs up

Thank you all.

Because there were two separate anonymous functions,
each with their own "onload()" function I thought it was required as written.

Solution:
I put both anonymous functions just before the end </body> tag
and removed the internal "onload()" commands and left the code they controlled alone.

I'll post the demo code if anyone is interested as to what I did.
jmrker is offline   Reply With Quote
Old 03-13-2013, 04:11 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,992 Times in 3,961 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The onload would have been required if the anonymous functions were, for example, in the <head>. But then, of course, you would have needed to use something other than just onload if there were two.

Fun, isn't it?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-13-2013, 04:33 AM   PM User | #7
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Thumbs up

Quote:
Originally Posted by Old Pedant View Post
The onload would have been required if the anonymous functions were, for example, in the <head>. But then, of course, you would have needed to use something other than just onload if there were two.

Fun, isn't it?
On occasion.

BTW: Is there a way to mark the thread as resolved? I cannot find a button for this.
jmrker is offline   Reply With Quote
Old 03-13-2013, 04:39 AM   PM User | #8
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by jmrker View Post
On occasion.

BTW: Is there a way to mark the thread as resolved? I cannot find a button for this.
funny, i was thinking the same thing the other day...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 03-13-2013, 08:30 AM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jmrker View Post
On occasion.

BTW: Is there a way to mark the thread as resolved? I cannot find a button for this.
Edit your original post. Go advanced.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:57 AM.


Advertisement
Log in to turn off these ads.