0

I returned the rsltCallback function , when i call the googleSearchSuggestions function , i am getting undefined. When i console.log the input parameter inside the rsltCallback function it's printing the output to console.

  var googleSearchSuggestions = function(search_Keyword , element) {


    var parsed;

    var  uri = 'http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=' + search_Keyword;

    var xhr = (typeof XMLHttpRequest !== 'undefined') ? new XMLHttpRequest() : new ActiveXObject(Microsoft.XMLHTTP);



    xhr.onreadystatechange = function(){

    xhr.responseType = 'xml';



    if(xhr.status == 200 && xhr.readyState == 4)
    {

      var response = xhr.responseXML;     

         var items = response.getElementsByTagName('toplevel')

         for (var i = 0 ; i < items[0].childNodes.length;i++)
         {




           parsed = items[0].childNodes[i].firstElementChild.getAttribute('data');

           rsltcallBack(parsed);



         }




     } 



    };

xhr.open('GET', decodeURI(uri), true);

xhr.send();

var rsltcallBack = function(input){

    return input;


};

    return rsltCallBack();

};
Jijo John
  • 1,375
  • 9
  • 21

5 Answers5

0

Because you have an asynchronous function call in your method you need to return the parsedValue at a later time. A way to do this are so called promises.

If you want that variable to be globally accessible then you can just add it to window (eg. window.parsedOutput = ...).

Jonathan
  • 3,614
  • 3
  • 21
  • 30
0

Instead of accessing local variable, just initialize the variable outside the function and assign values locally, Copy-Paste this,

var parsed ="";
var googleSearchSuggestions = function(search_Keyword , element) {
var  uri = 'http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=' + search_Keyword;

var xhr = (typeof XMLHttpRequest !== 'undefined') ? new XMLHttpRequest() : new ActiveXObject(Microsoft.XMLHTTP);



xhr.onreadystatechange = function(){

xhr.responseType = 'xml';

xhr.open('GET', decodeURI(uri), true);

xhr.send(); 

if(xhr.status == 200 && xhr.readyState == 4)
{

  var response = xhr.responseXML;     

     var items = response.getElementsByTagName('toplevel')


     for (var i = 0 ; i < items[0].childNodes.length;i++)
     {




       parsed = items[0].childNodes[i].firstElementChild.getAttribute('data');

       rsltcallBack(parsed);



     }






 } 



}

};



function rsltcallBack(input)
{
 setTimeout(function()
 {   
   alert(input);
 },2000);

}
Nandan Bhat
  • 1,573
  • 2
  • 9
  • 21
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/76063/discussion-on-answer-by-nandan-how-to-access-the-values-of-local-variables-globa). – Taryn Apr 23 '15 at 13:23
0

There are a few ways to declare global variables

var carName;  

function myFunction() {    
    carName = 'Volvo';    
}

This example declares the variable outside of the function and then assigns a value to it inside the function scope.

The other is

function myFunction() {    
        window.carName = 'Volvo';    
    }

This sets the variable on the window which can be deleted using delete window.carName.

Noel Kriegler
  • 509
  • 3
  • 11
0
This incident as JavaScript Variable hosting issue. for ex:

    var value = "a";

function test() {
    var value;
   alert(value)

}

test();

the global variable return undefined error. same code as works fine as below

    var value = 10;

function test() {
    var value ="b";
   alert(value);

}

test();

online reference : 

http://techslides.com/understanding-javascript-closures-and-scope

    http://stackoverflow.com/questions/9085839/surprised-that-global-variable-has-undefined-value-in-javascript
J.M.Farook
  • 183
  • 1
  • 8
0

An onreadystatechange function is only called after the state has been changed. This is not immediate; there is a split second in which the state has not yet changed. So you cannot access parsedOutput until the onreadystatechange function is called; its value has not yet been set. JavaScript doesn't just stop and wait for this to happen; it allows the request to be made in the background. It continues executing your code, even though the onreadystatechange method has not yet been called.

To solve your problem, simply wait for a response before using parsedOutput. You do this by using the onreadystatechange function to trigger the next step in your code.

BTW, performing a request in the background like this is call an asynchronous request. It is how you make remote requests with JavaScript; it prevents the script from hanging while waiting for a response.

Nathan K
  • 316
  • 3
  • 6