javascript - XMLHttpRequest() returning response in a function -
javascript - XMLHttpRequest() returning response in a function -
this question has reply here:
how homecoming response asynchronous call? 11 answersi have function isonline(), looks this:
function isonline() { var request=new xmlhttprequest(); request.onreadystatechange=function() { if(request.readystate==4) { if(request.responsetext=="online") homecoming true; } } request.open("get","onlinecheck.php?user=user",false); request.send(); homecoming false; } if run document.write(isonline()); testing , false, (undefined if dont write return false; , undefined.
how 'wait' before readystate 4 , homecoming true after ?
you need focus on few things:
onreadystatechange used when sending asynchronous request - more here. so if trying send asynchronous request, utilize async=true in phone call - read more here.
request.open("get", "onlinecheck.php?user=user", true); finally, if going utilize asynchronous call, can't homecoming value. check these 2 links
returning values event onreadystatechange in ajax how homecoming value asynchronous callback function?in case decide synchronously:
function isonline() { var request=new xmlhttprequest(); request.open("get","www.google.com/",false); request.send(); /* check readystate, can check status code */ if (request.readystate === 4) // set other conditions here if need homecoming true; else homecoming false; } // returns true javascript php ajax xmlhttprequest
Comments
Post a Comment