function getFile(url) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
return xhr.responseText;
}
}
xhr.open('GET', url);
xhr.send();
}
function somefunc() {
return getFile('http://domain/n.php');
}
console.log(somefunc()) //undefined
function getFile(url, cb) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
if (typeof cb === 'function') {
cb(xhr.responseText);
}
}
}
xhr.open('GET', url);
xhr.send();
}
return getFile('http://domain/n.php', function (data) { console.log(data); });