function makeAjaxRequest(destination_full_url) {
if (window.XMLHttpRequest) {// code for modern browsers
xmlhttp = new XMLHttpRequest();
} else {// code for old Internet Explorer versions
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert("Response: \n" + xmlhttp.responseText);
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", destination_full_url, true);
xmlhttp.send();
}
function callThirdPartyFunction(obj) {
var response;
response = makeAjaxRequest("info.php?keyword=obj.value");
alert(response);
}
<textarea onkeypress="callThirdPartyFunction(this);"></textarea>
A "undefined" message box shows up first and then a message box with the ajax request data appears.
The question is why the alert(response); runs before the ajax_request() function finishes and how to make it wait until the function is complete before moving to the next line?