Confused by order of the Javascript AJAX example -
this very easy i'm stuck understanding logic. trying understand how ajax works , found following example in w3schools.com. examples of few other sites more or less same.
<!doctype html> <html> <body> <h2>retrieve data xml file</h2> <p><b>status:</b> <span id="a1"></span></p> <p><b>status text:</b> <span id="a2"></span></p> <p><b>response:</b> <span id="a3"></span></p> <button onclick="loaddoc('note.xml')">get xml data</button> <script>
so, @ point, clicked button , function fired up.
function loaddoc(url) { var xhttp = new xmlhttprequest(); xhttp.onreadystatechange = function() { if (xhttp.readystate == 4 && xhttp.status == 200) { document.getelementbyid('a1').innerhtml = xhttp.status; document.getelementbyid('a2').innerhtml = xhttp.statustext;
at point, trying fill a3 span answer server. don't know url yet!
document.getelementbyid('a3').innerhtml = xhttp.responsetext; } };
and here have our url,
xhttp.open("get", url, true);
we send request
xhttp.send(); } </script>
aaand credits.
</body> </html>
to surprise, works without hitch. after setting our url, did not instructed browser update div. did before introduced our url , @ point had no data server.
i not head around it. logic of this? anonymous function (the xhttp.onreadystatechange one) activated along xhttp.send() command? or onreadystatechange command loops until conditions met?
the
xmlhttprequest.onreadystatechange
property contains event handler called whenreadystatechange
event fired, every timereadystate
property ofxmlhttprequest
changes. callback called user interface thread.the
readystatechange
event not fired when xmlhttprequest request canceled abort() method.
it not keep looping, fired when readystate value updated. readystate has values of 0,1,2,3,4. 0 when open()
has not been called. setting before open
not big deal, not need in order.
Comments
Post a Comment