You can not.
None of them execute synchronous code, and you should not return anything loadXMLDoc , but an anonymous function, which is the onreadystatechange handler.
Your best shot is to pass a callback function.
function loadXMLDoc(myurl, cb){ var xmlhttp; if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest();} else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ if( typeof cb === 'function' ) cb(xmlhttp.responseText); } } xmlhttp.open("GET",myurl,true); xmlhttp.send(); }
And then call it like
loadXMLDoc('/foobar.php', function(responseText) {
source share