Asynchronous—True or False?
AJAX stands for Asynchronous JavaScript and XML, and for the XMLHttpRequest object to behave as AJAX, the async parameter of the open method has to be set to true:
xmlhttp.open( "GET", "ajax_test.asp", true );
Many of the tasks performed on the server are very time consuming.
Before AJAX, this operation could cause the application to hang or stop.
With AJAX, the JavaScript does not have to wait for the server response, but can instead:
execute other scripts while waiting for server response and
deal with the response when the response ready.
Async = true
When using async=true, specify a function to execute when the response is ready in the onreadystatechange event:
Async = false
To use async=false, set the third parameter in the open method to false:
xmlhttp.open( "GET", "ajax_info.txt", false );
Using async=false is not recommended because the JavaScript will NOT continue to execute, until the server response is ready.
If the server is busy or slow, the application will hang or stop.
Note that when you use async=false, do NOT write an onreadystatechange function—just put the code after the send statement:
Let AJAX change this text.
<script type="text/javascript">
function loadXMLDoc( ) {
var xmlhttp;
if ( window.XMLHttpRequest ) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest( );
}
else {
// code for IE6, IE5
xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
}
xmlhttp.open( "GET", "AJAX_info.txt", false );
xmlhttp.send( );
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
</script>