Returning variable from onreadystatechange
<p>I am trying to return a variable from onreadystatechange and seem to be having trouble with scope. Even if I set a global variable, when trying to retrieve my variable after the fact, its undefined. Not quite sure what I am doing wrong. I haven't done too much work with Javascript in the past 3 years.</p>

<pre><code>var lat;
var lon;

 window.onload = function dosomething(){

  var myRequst = new XMLHttpRequest();

  var url = "http://www.instamapper.com/api?action=getPositions&amp;key=584014439054448247";

  myRequst.open('get', url);

  myRequst.onreadystatechange = function(){

  if ((myRequst.readyState == 4) &amp;&amp; (myRequst.status == 200)){

  //alert(myRequst.responseText);

   var data = myRequst.responseText;
   collected=data.split(",");   //parses the data delimited by comma and put data into array

lat = collected[4];
lon = collected[5];

document.write("latitude "+lat+"&lt;BR&gt;\n");
document.write("latitude "+lon+"&lt;BR&gt;\n");

  }

  }

  myRequst.send(null);


 }

document.write(lat);
</code></pre>

<p>Any help or suggestions would be much appreciated.</p>
