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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question multiple window.onload function issue

    I have Javascript functions called start() that have multiple functions to load using window.onload function. However, I have below function that works independently fine. However, if I write it inside the window.onload, then it doesn't work.
    PHP Code:
    //START()     
    window.onload start;

    function 
    start()
    {
        
    loadValues();
        
    showState4();

    Code that does work independently fine.

    PHP Code:
    window.onload=function(){
        
    document.getElementById("src2TargetAll").onclick = function() {
            
    sureTransfer(document.getElementById("net"), document.getElementById("target"), true);
        };
    }; 
    I tried re-writing the code as follows in window.onload but it doesn't work. How to re-write the below code in single window.onload function.

    PHP Code:

    window
    .onload start;

    function 
    start()
    {
        
    loadValues();
        
    showState4();
        
    sendValuess();
    }


    function 
    sendValuess(){
        
    document.getElementById("src2TargetAll").onclick = function() {
            
    sureTransfer(document.getElementById("net"), document.getElementById("target"), true);
        };
    }; 
    Error that I get after adding sendValues() to window.onload is as follows:



    STOP RUNNING THIS SCRIPT?
    A SCRIPT ON THIS PAGE IS CAUSING YOUR WEB BROWSER TO RUN SLOWLY. IF IT CONTINUES TO RUN, YOUR COMPUTER MIGHT BECOME UNRESPONSIVE.

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by naveendk.55 View Post
    I tried re-writing the code as follows in window.onload but it doesn't work. How to re-write the below code in single window.onload function.
    There's nothing wrong with the onload handler as such; the problem must be in one of the functions that you're calling but haven't shown.

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    below is the code for loadValues and other functions

    PHP Code:
    function showState4(){
            var 
    me document.getElementById('stk1');
             var 
    values ''//populate selected options
             
    for (var i=0i<me.lengthi++)
                 if (
    me.options[i].selected)
                     
    values += me.options[i].value ',';
             
    values values.substring(0values.length-1);
             var 
    selected=[values];

             var 
    temp= new Array();
                
    temp values.split(",");

             var 
    del document.getElementById('StakeHolder');

             for(var 
    i=0i<del.lengthi++)
               {
                 for(var 
    j=0;j<temp.length;j++)
                  {  

                      if(
    temp[j] == del.options[i].value)
                        {

                           
    del.options[i].selected true;
                        }
                   }  
               }
             }

         function 
    loadValues()
         {
        var  
    RD_REQ_RT_ID "<%=RD_REQ_RT_ID %>";
        if(
    RD_REQ_RT_ID=="null")
         {
        
    document.getElementById('requestType').value="";
         }
         else{
        
    document.getElementById('requestType').value=RD_REQ_RT_ID;
        }
        )


    _

         
    function sureTransfer(fromtoall) {
        if ( 
    from.getElementsByTagName && to.appendChild ) {
            while ( 
    getCount(from, !all) > ) {
                
    transfer(fromtoall);
            }
        }
         }
         function 
    getCount(targetisSelected) {
        var 
    options target.getElementsByTagName("option");
        if ( !
    isSelected ) {
            return 
    options.length;
        }
        var 
    count 0;
        for ( 
    0options.lengthi++ ) {
            if ( 
    isSelected && options[i].selected ) {
                
    count++;
            }
        }
        return 
    count;
        }
        function 
    transfer(fromtoall) {
        if ( 
    from.getElementsByTagName && to.appendChild ) {
            var 
    options from.getElementsByTagName("option");
            for ( 
    0options.lengthi++ ) {
                if ( 
    all ) {
                    
    to.appendChild(options[i]);
                } else {
                    if ( 
    options[i].selected ) {
                        
    to.appendChild(options[i]);
                    }
                }
            }
        }
         } 

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    I would guess that one of your loops isn't being allowed to termninate, probably this:

    Code:
      while ( getCount(from, !all) > 0 ) {
                transfer(from, to, all);
            }
    if I understand, when getCount is passed !all (false) it will always return options.length, which if non-zero will give an infinite loop.


    Also you have two functions sharing a global loop variable i, probably not the cause but fix it.

  • #5
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    loadValues() and showState4() works fine without sendValues() in window.onload function. Also, I tested sendValues() without above two functions and that also works fine. but three functions in window.onload creates a problem. So it should not be due to the while loop. However, if any code change that you suggest on while loop, then let me know. I am willing to give it a try.

  • #6
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Sometimes debugging can be simple. Just comment the loop out and see if it works. If it does, the loop is the problem. Debugging without even so much as trying to understand the code.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Quote Originally Posted by naveendk.55 View Post
    loadValues() and showState4() works fine without sendValues() in window.onload function. Also, I tested sendValues() without above two functions and that also works fine. but three functions in window.onload creates a problem. So it should not be due to the while loop. However, if any code change that you suggest on while loop, then let me know. I am willing to give it a try.
    That sounds like you have sendValues() using the same global variable or variables as the other functions use but for a different purpose.
    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.


  •  

    Tags for this Thread

    Posting Permissions

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