Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2013, 02:32 PM   PM User | #1
naveendk.55
New Coder

 
Join Date: Aug 2011
Posts: 88
Thanks: 5
Thanked 0 Times in 0 Posts
naveendk.55 is an unknown quantity at this point
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:


Quote:

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.
naveendk.55 is offline   Reply With Quote
Old 01-27-2013, 02:53 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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.
Logic Ali is offline   Reply With Quote
Old 01-27-2013, 02:56 PM   PM User | #3
naveendk.55
New Coder

 
Join Date: Aug 2011
Posts: 88
Thanks: 5
Thanked 0 Times in 0 Posts
naveendk.55 is an unknown quantity at this point
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]);
                }
            }
        }
    }
     } 
naveendk.55 is offline   Reply With Quote
Old 01-27-2013, 04:47 PM   PM User | #4
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
I would guess that one of your loops isn't being allowed to termninate, probably this:

Quote:
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.
Logic Ali is offline   Reply With Quote
Old 01-27-2013, 05:50 PM   PM User | #5
naveendk.55
New Coder

 
Join Date: Aug 2011
Posts: 88
Thanks: 5
Thanked 0 Times in 0 Posts
naveendk.55 is an unknown quantity at this point
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.
naveendk.55 is offline   Reply With Quote
Old 01-27-2013, 06:46 PM   PM User | #6
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 367
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
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.
Airblader is online now   Reply With Quote
Old 01-27-2013, 09:39 PM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:50 PM.


Advertisement
Log in to turn off these ads.