saving XMLHttpRequest.responseText in a variable
<p>I'm writing a small script in JS to save the data using a php website like this:</p>

<pre><code>function getPopulation(id) {
    var xhr_object = null;

    if(window.XMLHttpRequest) // Firefox
       xhr_object = new XMLHttpRequest();
    else if(window.ActiveXObject) // Internet Explorer
       xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
    else { // XMLHttpRequest non support? par le navigateur
       alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
       return;
    }

    var url = "http://localhost/inf347/td-svg/population.php?id=1";
    var res = 0;

    xhr_object.open("GET", url,true);
    xhr_object.send(null) ;
    xhr_object.onreadystatechange=function() {
        if (xhr_object.readyState==4) {
                //alert(xhr_object.responseText) ;
                res =   xhr_object.responseText ;
        }
    }
    alert(res);
    //return -1;
}
</code></pre>

<p>If I uncomment the line "//alert(xhr_object.responseText) ;" then the program prints the correct answer. But there is no way to save that value into a variable.
Does anyone know how to get round it?</p>

<p>Thanks,
Son</p>
