
- Simple Javascript is the key to Ajax - Mark Alexander Bain
The most revolutionary thing about Ajax (Asynchronous JavaScript and XML) is not that it's a brand new technology - it's not: the techniques for asynchronous web content loading have existed for more than 10 years. No, the revolutionary thing about Ajax is the way that these techniques have been adopted throughout the world as a web standard.
The second most revolutionary thing about Ajax is that it's very easy to implement.
The Technology Behind Ajax
The technology used by the Ajax technique is quite simple (from a web developer's point of view); it makes use of:
- the XML DOM (Extensible Markup Language Document Object Model) which defines the standard for manipulating the documents that makes up web pages - it also allows for asynchronous updating of sections of the web page
- the XML HTTP Request Object - this is defined as part of the DOM, and allows for asynchronous communication with a server.
And its the way that these objects have been implemented by web browser developers (such as Microsoft and Mozilla) that makes Ajax so useful.
Although there's an important point to take note of here - it's not necessary to develop sever applications in order to benefit from using Ajax.
Ajax Without a Server
The simplest Ajax application doesn't need to have access to a server (apart, of course, from the hosting of the web site itself); in this case the Ajax application just makes use of the DOM's ability to update areas of the web page asynchronously:
This is a simple example, but shows how the same area of the screen (the choice_area div) can be updated by just clicking a radio button, and without having to update the whole web page.
Ajax With a Server: Using the XML HTTP Request Object
The first thing that's needed is a script to run on the server, and this can be anything that is able to accept an input and return a result, so that could (for example) be:
- ASP
- CGI
- PHP
In this case it's a PHP script that returns the date in different formats:
If that is saved as show_date.php then it can be used in the following Ajax web page:
There's an important point to take note of here - the first task for the Javascript to do is to create the XML HTTP Request Object; however, how this object is accessed depends on the browser being used: Microsoft does it one way, everyone else does it another. The script, therefore, tests to see whether or not a Microsoft browser is being used, and then calls the appropriate object.
Once Javascript is using the correct object then it's just a matter of sending any inputs to the server script and processing the result (i.e. displaying them in the web page).
Conclusion
Ajax is just a simple technique, but it's a technique that has revolutionised the way that web sites operate. Fortunately it's a painless revolution that every web page developer can benefit from.
