For simplicity, this page works only in FireFox / Mozilla. This is meant only as a demonstration of the basic XMLHTTPRequest object functionality. Further examples will use cross-platform libraries.
This example uses asynchronous response-handling asynchronous responses are less prone to hanging the browser, but they require an event-handler, because the asynchronous response doesn't block the code on the client as the synchronous version does. You must set up an event handler to catch the response before you open the connection.
The event handler needs to check for a readystate of 4 to know that the transmission is complete. There's no need to process the information (in most cases) until the transmission is finished.
function getAJAX(){
var request = new XMLHttpRequest();
// if third parameter is left out, it's an asynchronous request
request.open("GET", "test.html");
// create an anonymous event handler to receive
// state change (like a keyboard reader)
request.onreadystatechange = function(){
if (request.readyState == 4) {
// if state is finished
if (request.status == 200) {
// and if attempt was successful
alert(request.responseText);
} // end if
} // end if
} // end if
request.send(null);
} // end function