Having trouble retrieving a string value from this function:
function loadPage(url) {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, true);
xhttp.send();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//var html = xhttp.responseText
var html = document.documentElement.outerHTML;
return html
}
}
}
console.log(loadPage("http://stops.lt/vilnius/#trol/2/a-b"))
Although console.log(html)
prints the correct results within the xhttp.onreadystatechange
, I'm struggling to understand how to return loadPage(url)
. I've tried both return xhttp.onreadystatechange
and
return xhttp.onreadystatechange()
, but neither of them seem to work.