dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: onload with multiple arguments?

  1. #1
    Join Date
    May 2005
    Posts
    26

    onload with multiple arguments?

    I have two javascripts that need to be initiated with onload. Looking at the w3c school information I can't find informaton on how to do this. I tried onload= "func1, func2" and "func1", "func2" without success. Can this be done and what is the correct syntax?

  2. #2
    Join Date
    Aug 2011
    Location
    Worthing, West Sussex
    Posts
    96
    Easy, make a function that loads your other functions, for example:

    HTML:
    HTML Code:
    <body onload="loadme()"> ... </body>
    JS:
    HTML Code:
    <script type="javascript/text">
    function loadme() {
      function_a();
      function_b();
    }
    </script>
    Alternatively you could use jQuery to load these events:

    HTML Code:
    $(document).ready(function() {
      function_a();
      function_b();
    });
    OR, you could use the same thing in JS:

    HTML Code:
    <script type="javascript/text">
    window.onload = load me();
    </script>

  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    Quote Originally Posted by dangerousprince View Post
    HTML Code:
    <script type="javascript/text">
    window.onload = load me();
    </script>
    That does not do what you think that it does. As the piece of script is parsed, id est long before the onload event is triggered, loadMe will be called and whatever is returned will be assigned to the onload handler. In this case undefined is returned (or possible null, my JavaScript theory has become a bit rusty since I was banned from that forum) so on load the browser will try to do undefined (or do null). It'll either do nothing or throw an error. The function loadMe will be called, but it will be called before the page is loaded. You want:
    HTML Code:
    <script type="text/javascript">
    window.onload = loadMe
    </script>
    But for simplicity sake I would use
    HTML Code:
    window.onload = function () {
      function_a()
      function_b()
    }
    Or even better
    HTML Code:
    <body onload="function_a(); function_b()">
    Last edited by Charles; 08-10-2011 at 07:29 PM.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  4. #4
    Join Date
    Aug 2011
    Location
    Worthing, West Sussex
    Posts
    96
    Whoops, my mistake on putting a space in 'load me();' - it will work correctly how I showed - bar the space!

    But what Charles quoted, is the one you're looking for:
    HTML Code:
    <body onload="function_a(); function_b()">

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    Quote Originally Posted by dangerousprince View Post
    Whoops, my mistake on putting a space in 'load me();' - it will work correctly how I showed - bar the space!

    But what Charles quoted, is the one you're looking for:
    HTML Code:
    <body onload="function_a(); function_b()">
    The space is not the problem. The problem is the "()". That calls the function and then what is returned is assigned. You want to assign the function itself to the handler. In JavaScript you're really creating methods not functions and methods are really properties and can be assigned. The function is called either way, it's just a question of when and what error is thrown.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  6. #6
    Join Date
    Aug 2011
    Location
    Worthing, West Sussex
    Posts
    96
    Ah, yes. Very true, when used in the window.load, no brackets.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles