Variable scope problem in JavaScript

Posted by dfjhdfjhdf on Stack Overflow See other posts from Stack Overflow or by dfjhdfjhdf
Published on 2010-06-02T09:05:27Z Indexed on 2010/06/02 9:13 UTC
Read the original article Hit count: 204

Filed under:
|

I declare a variable with the var word inside a function that handles Ajax requests, then later on in the function I have another function that should change the value of the variable but - my problem - it fails. How to settle the problem down? Here's similiar code I use:

function sendRuest(someargums) {
     /* some code */

     var the_variable;

     /* some code */

     //here's that other function 
     request.onreadystatechange =   
        function() {                
            if (request.readyState == 4) {    
                switch (request.status) {
                    case 200:
                        //here the variable should be changed
                        the_variable = request.responseXML;

        /* a lot of code */ 

        //somewhere here the function closes
        }

     return the_variable;
}

var data = sendRequest(someargums); //and trying to read the data I get the undefined value

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about variable-scope