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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    combining more than one 'on load' tag in body

    hey i just have a simple question... how can i do more than one "on load" tag in the body... this is what i have so far, i know you can't have more that one body tag but i want to combine the on loads...

    <body onload="menuSlider.init('menu','slide')">
    <body onload="changePhoto(0)">


    thanks

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    I believe you'd do something along the line of.
    Code:
    <body onload="menuSlider.ini('menu', 'slide'); changePhoto(0);">
    But don't quote me, because I usually use event listeners e.g. in jQuery.
    Code:
    $(function(){
      // code
    });
    
    // Or
    $(document).ready(function(){
      // code
    });
    Undoubtedly you could also do
    Code:
    $('body').load(function(){
      // code
    });
    That said you should try and use event listeners, it's much easier to manage in my opinion and furthermore it's separating the layers (content, presentational and finally behavioral)

  • Users who have thanked Iszak for this post:

    oleg2 (12-10-2009)

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I agree with Iszak, and especially since you posted this question in the jQuery forum, may as well take advantage of jQuery's awesomeness.

    But if you really really want to use Javascript only, here's a function I found a while ago that makes it easy to kick off two functions with the onload event.

    PHP Code:
    function addLoadEvent(func) {
        var 
    oldonload window.onload;
        if (
    typeof window.onload != 'function') {
            
    window.onload func;
        } else {
            
    window.onload = function() {
            if (
    oldonload) {
                
    oldonload();
            }
            
    func();
            }
        }

    Then you simply call this addLoadEvent() function multiple times.

    PHP Code:
    addLoadEvent (doSomething);
    addLoadEvent (doSomethingElse); 
    I found the original page I stole this from, it has all the info you need to get it working properly.

    http://www.htmlgoodies.com/beyond/ja...le.php/3724571

  • Users who have thanked Fumigator for this post:

    oleg2 (12-10-2009)

  • #4
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks a lot fellas it worked... yeh the only reason im doin it in javascript is that its for an assignment and not allowed to use jquery

  • #5
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    350
    Thanks
    9
    Thanked 51 Times in 50 Posts
    How about using addEventListener() ->http://www.w3.org/TR/DOM-Level-2-Events/events.html?

    But this only to browsers with DOM2 supported (as usual... IE use other non-standard method) and should remove it using removeEventListener() later?

    Regards,
    Last edited by hdewantara; 12-11-2009 at 10:20 PM.


  •  

    Posting Permissions

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