Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    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.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 591 Times in 572 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,421 Times in 4,386 Posts
    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.

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 591 Times in 572 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •