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 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    multiple functions called onload

    I would like to call several sort of 'init' type functions when a document loads. I understand that listing several for window.onload doesn't work because each new one overwrites the previous or something to that effect, yes?

    I did some searching and it appears that to achieve this I either need to a) list functions in the <body> onload attribute or b) attach events using JavaScript or something to that effect.

    I am interested in the latter because of my interest in keeping behavior seperate from the structure. From what I've been able to gather, this is a purist approach which is hard to get universal functionality for, but maybe I do not understand correctly.

    I'm ultimately just looking for the smartest, simplest approach to get the desired result. Any pointers or references?

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <script type="text/javascript" language="javascript" src="source.js"></script>

    or

    <body onLoad="javascript:myFunc1();myFunc2();myFunc3();">



    -james
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or:

    function duitt(){
    myFunc1();
    myFunc2();
    myFunc3();
    }
    window.onload = duitt;

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Or:
    Code:
    window.onload = function(){
      myFunc1();
      myFunc2();
      myFunc3();
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or:
    Code:
    function registerEvent (obj, trigger, handler) {
       if (obj.addEventListener) {
          obj.addEventListener(trigger, handler, false);
       } else if (obj.attachEvent) {
          obj.attachEvent('on' + trigger, handler);
       } else {
          if (!obj['on' + trigger]) {
             obj['on' + trigger] = function () {
                if (!this[trigger + 'handlers']) {
                   return false;
                }
                for (var ix = 0; this[trigger + 'handlers'].length > ix; ix++) {
                   this[trigger + 'handlers'][ix]();
                }
             }
          }
          if (!obj[trigger + 'handlers']) {
             obj[trigger + 'handlers'] = [];
          }
          obj[trigger + 'handlers'][obj[trigger + 'handlers'].length] = handler;
       }
    }
    then:
    Code:
    registerEvent(window, 'load', function1);
    registerEvent(window, 'load', function2);
    registerEvent(window, 'load', function3);
    I'm not using a for-in loop or the Array.push() method because (IIRC) they aren't supported by several browsers that require that backward-compatible multiple-handler-per-event assignment. (This is untested, and I'm sure needs refinement; it's similar to one I wrote for Netscape 6 and IE 6 pre-SP1)

  • #6
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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