One Last Word on Asynchronous Versus Synchronous
Though the "A" in Ajax stands for "asynchronous," the XMLHttpRequest object doesn't have to be used asynchronously. A synchronous request is one where the browser is locked up waiting for a response. An example of this behavior would be the following:
var state = document.forms[0].elements[0].value;
var url = 'ajax.php?state=' + state;
xmlhttp.open('GET', url, false);
xmlhttp.send(null);
alert(xmlhttp.getAllResponseHeaders( ));In this code, a string representing a state is accessed from a form field and used to build an HTTP GET request on the web service, ajax.php. The third parameter in the open method is false, which means the request is made synchronously. The request is sent without any parameters (the null value), and immediately after the send, an alert message box is opened and the response headers are printed out.
Now, if the service is quick, the response is almost immediate. However, if the service is slow, no other page activity can happen until the request finishes. The effect is little different from when you first open a web page and you can't typically do anything with the page until it's finished loading.
Most Ajax applications use the XMLHttpRequest object asynchronously. In this case, an asynchronous request is sent, but the browser continues processing the code following the request rather than waiting for a response from the server. To process the response when it is sent, a function is assigned to the onreadystatechange ...