If I am within my XHR's onreadystatechange function, I can easily do document.title = xhr.responseText
, but if I make the function return the responseText, I can't set a variable equal to my XHR's outerlying wrapper to make it equal the response; is there any way to go about doing this?
My wrapper:
ajax = function(url, cb)
{
xhr = (window.XMLHttpRequest)
? new XMLHttpRequest()
: new ActiveXObject('Microsoft.XMLHTTP');
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4 && xhr.status == 200)
{
cb(xhr.responseText);
};
}
xhr.open('get', url, true);
xhr.send();
};
Now, if I did something like:
ajax('bacon.txt', function(_)
{
document.title = _;
}
it works absolutely perfectly; document.title does in fact become the responseText of the call to bacon.txt. If, however, I try to implement it this way:
document.title = ajax('bacon.txt', function(_)
{
return _;
}
no such luck. Can anybody clarify as to why this is? };