SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to combine two window.onload?

    I have two window.onload in one page, one of them can not run well
    HTML Code:
    <script>
    function aaa(...)
    ...
    
    window.onload=function(){
    var menucount...;
    if ...;}
    }
    </script>
    <body onload="menu.list('div2')">
    I have read some tutorial like:
    HTML Code:
    function start() {
      func1();
      func2();
    }
    window.onload = start;
    But I still don't know how to use in my code. one in <script>, another in <body>. So is there anyone can help me? Thanks.

  2. #2
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is a JavaScript question but window.onload is not reliable, body onload is a better bet and you can call several onload events within the body tag.
    What I lack in acuracy I make up for in misteaks

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try to call the function at the end of your webpage.

    For exammple:
    Code:
    </body>
    </html>
    <script>
    window.onload = start();
    </script>

  4. #4
    SitePoint Member
    Join Date
    Nov 2010
    Location
    florida
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Couldn't he also simply do this as well?


    Code:
    <script>
    function start()
    {
    func1();
    func2();
    
    }
    
    function func1()
    {
    document.write("Hellow World!");
    }
    function func2()
    {
    document.write("\nHello World 2!");
    }
    
    window.onload = start;
    
    </script>

    Works fine for me.

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for a quickly reply.
    I tried to add
    <script>
    window.onload = start();
    </script>
    after </html>, but it not work. Is there need modify other codes?

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GulloRichard View Post
    Couldn't he also simply do this as well?


    Code:
    <script>
    function start()
    {
    func1();
    func2();
    
    }
    
    function func1()
    {
    document.write("Hellow World!");
    }
    function func2()
    {
    document.write("\nHello World 2!");
    }
    
    window.onload = start;
    
    </script>

    Works fine for me.
    Can you put my code in it for me? I don't whether should modify the code in <body>? Thanks.

  7. #7
    SitePoint Member
    Join Date
    Nov 2010
    Location
    florida
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For me to help you, I need to see your code. Post the whole code and I'll set it up for you.

  8. #8
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lorenw View Post
    This is a JavaScript question but window.onload is not reliable, body onload is a better bet and you can call several onload events within the body tag.


    Tag event handlers should not be used. Ever.

    I'll repeat that.

    Tag event handlers should not be used. Ever.

    They pollute the HTML with javascript code, which is everybit as bad as putting business logic PHP into a template file - maybe even worse because the PHP has the good grace to disappear once the template eval's, and the javascript cannot escape.

    Events should be registered to the window onload event, but rather than worry about the stupidity that is the world of 101 browsers, delegate the problem to a Javascript framework.

    In Prototype.js

    Code javascript:
    Event.observe( window, 'load', function() {
      // My window event code goes here.
    } );

    In jQuery

    Code javascript:
    $(document).ready(function() {
      // Handler for .ready() called.
    });

    Both of these have an important advantage over the onload event handler - they don't fire until all the objects of the page have loaded, which may be important if you have multiple scripts or lengthy CSS to process. You can also attach as many events as possible.

    The onX handlers though should be treated as deprecated. They create far more problems than they solve.

    Now that said, reporting original post to have it moved to the javascript forum. This has nothing whatsoever to do with PHP.

  9. #9
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script type="text/javascript">
    //Set the id names of your tablink (without a number at the end)
    var tablink_idname = new Array("tablink")
    //Set the id name of your tabcontentarea (without a number at the end)
    var tabcontent_idname = new Array("tabcontent") 
    //Set the number of your tabs
    var tabcount = new Array("3")
    //Set the Tab wich should load at start (In this Example:Tab 2 visible on load)
    var loadtabs = new Array("1")  
    //Set the Number of the Menu which should autochange (if you dont't want to have a change menu set it to 0)
    var autochangemenu = 0;
    //the speed in seconds when the tabs should change
    var changespeed = 10;
    //should the autochange stop if the user hover over a tab from the autochangemenu? 0=no 1=yes
    var stoponhover = 0;
    //END MENU SETTINGS
    
    /*Swich EasyTabs Functions - no need to edit something here*/
    function easytabs(menunr, active) {if (menunr == autochangemenu){currenttab=active;}if ((menunr == autochangemenu)&&(stoponhover==1)) {stop_autochange()} else if ((menunr == autochangemenu)&&(stoponhover==0))  {counter=0;}menunr = menunr-1;for (i=1; i <= tabcount[menunr]; i++){document.getElementById(tablink_idname[menunr]+i).className='tab'+i;document.getElementById(tabcontent_idname[menunr]+i).style.display = 'none';}document.getElementById(tablink_idname[menunr]+active).className='tab'+active+' tabactive';document.getElementById(tabcontent_idname[menunr]+active).style.display = 'block';}var timer; counter=0; var totaltabs=tabcount[autochangemenu-1];var currenttab=loadtabs[autochangemenu-1];function start_autochange(){counter=counter+1;timer=setTimeout("start_autochange()",1000);if (counter == changespeed+1) {currenttab++;if (currenttab>totaltabs) {currenttab=1}easytabs(autochangemenu,currenttab);restart_autochange();}}function restart_autochange(){clearTimeout(timer);counter=0;start_autochange();}function stop_autochange(){clearTimeout(timer);counter=0;}
    
    window.onload=function(){
    var menucount=loadtabs.length; var a = 0; var b = 1; do {easytabs(b, loadtabs[a]);  a++; b++;}while (b<=menucount);
    if (autochangemenu!=0){start_autochange();}
    }
    </script>
    <body onload="menu.list('div2')">
    These are the code in the page.
    There still have some js files.
    Thanks.

  10. #10
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have solved the problem. Thanks for everyone.


Tags for this Thread

Bookmarks

Posting Permissions

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