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 answers

i 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

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -