What is AJAX?

You can’t move in geek circles these days without hearing about AJAX but a lot of people I know seem only to have the vaguest ideas about what it is and what it does. I’m going to try to explain and give a few examples in what I hope will be two or three posts. To begin with: What Is AJAX?

AJAX stands for Asynchronous Javascript And XML, which probably isn’t much help – especially when you learn that it doesn’t have to be Asynchronous or use Javascript or XML. The "And" is fairly static, though.

Basically, AJAX is a method used on web pages for loading data from a server without doing a full post-back. In other words, you stay on the same page in your web browser and the data brought down in the background.

Although you won’t find the credit on Slashdot , the component at the heart of AJAX was invented by Microsoft who called it the IXMLHTTPRequest interface. In their version, the component is an ActiveX control. When the FireFox team (and others) came to write their own implementations, they made it a built-in part of the browser and called it XMLHttpRequest. Whichever version you use they both do the same thing.

To make an AJAX request, you need to create the Request component. Since this is sometimes an ActiveX object and sometimes a built-in component you obviously need at least two different methods to do this. Unfortunately, the ActiveX control is called different things in different versions of Windows, so you actually need four ways. This is the code I use to create the object.

var xmlhttp = false;

if (window.XMLHttpRequest) // Mozilla, Safari, etc
   xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject) // IE
   try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
   catch (e) {
      try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) {
         try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0"); } catch (e) {}

return xmlhttp;    

At the end of that code you have the variable xmlhttp that contains either the value false or the required object.

In the next post I’ll show you what you can do with it.

This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s