title: Returning values from the event onreadystatechange in AJAX
body: <p>I am trying to assign a value to the variable <code>val</code> in the code below:</p>
  
  <pre><code>var cmdValue = "/cmd/fetch?x=";
  var val;
  var returned_data;
  
  function what(){
      val = update('#TAG#');
  }
  
  
  function update(tag) {
      var req1 = newXMLHttpRequest();
      req1.open("GET",cmdValue + tag, true);
      req1.send("");
  
      return req1.onreadystatechange= function () {
          if (req1.readyState == 4 &amp;&amp; req1.status == 200) {
              returned_data = req1.responseText;
              return returned_data;
          }else{
  
          }
      };
  }
  </code></pre>
  
  <p>I was tracking the variables in Firebug and it turns out that <code>val</code> gets assigned the function. Is there a way to get the code to run through and then assign the value to the variable <code>val</code>? </p>