AJAX ("Asynchronous JavaScript and XML") is a technique that allows you to make calls to the server without reloading the page. The calls take place repeatedly in the background, allowing the server to return information to show new content in a seamless experience for the user.

Some common examples of AJAX include Google's auto-complete responses in the search field. As soon as you start typing in the search field, you see responses in real time, without any page refreshes.

Another example of AJAX is Google Maps. As you drag the screen in new areas, new map content loads from the server dynamically.

The infinite scrolling in Gmail and Google Reader also use AJAX functionality. As you scroll down, calls are made in the background to load new content from the server. The new content loads dynamically, without requiring page refreshes.

Two Components of an AJAX Call

Although making AJAX calls is more of an advanced technique in JavaScript, the following notes show the basic pattern so you can better recognize when AJAX is present.

AJAX uses a new object to make the calls: XMLHttpRequest. However, unlike other functions, AJAX splits the call and response into two separate steps. This division of steps with the call and response allows for the asynchronous element of AJAX.

The XMLHttpRequest Object

The first step is to create a new XMLHttpRequest object. Because of some cross-browser incompatibility in calling the XMLHttpRequest object, you have to create a conditional statement to account for Internet Explorer. With this conditional statement in mind, you create the new object as follows:

var sampleRequest;
//check the browser -- if a non-IE browser, create this object
if (window.XMLHttpRequest) {
    sampleRequest = new XMLHttpRequest();
//otherwise, if IE, create this object
} else if (window.ActiveXObject)  {
    sampleRequest = new ActiveXObject("Microsoft.XMLHTTP");

This above code doesn't specify exactly where on the server the request is being made. However, before you specify more details about the request, you have to specify how to handle the response that will come back from the server.

To handle this response, you use the onreadystatechange event to call an anonymous function. The basic format of this event in a JavaScript statement is as follows:

//apply the onreadystatechange method for this request
sampleRequest.onreadystatechange = function() {
//run some code here...

Now that you've defined how to handle the responses from the server, you can write a more specific request to send. The specific request might look as follows:

//specify the type of request and where to send it.
//"true" means as soon as you send,
//let browser to respond without waiting.
sampleRequest.open("GET","http://testsite.com/data.php", true);
//send the request. In parentheses, insert parameters.

AJAX calls are more advanced and would contain more detail, but this is the basic pattern of the call.

Get new posts delivered straight to your inbox.

Subscriber count: 3,220