11.10.07
A Specific Example of What You Can do with AJAX
AJAX (Asynchronous JavaScript and XML) is a programming language that was developed to do a number of really cool things. While this example does not contain any AJAX code, at the same time it will show you a real life situation where you can apply the use of the AJAX programming that you are going to learn.
In order to understand this situation in which AJAX can be used, it is important to first understand the code below:
<html>
<body>
<form name=”myForm”>
Name: <input type=”text” name=”username” />
Time: <input type=”text” name=”time” />
</form>
</body>
</html>
Now, this is pretty simplistic code. It is HTML code that creates a standard form with two fields; one for the name and one for the time. However, what is deceptively simple about looking at this code is the fact that you can use AJAX to fill in one of these two fields. The key way to do this is through the use of the XMLHttpRequest object, a JavaScript object that is basically the reason for AJAX’s existence. Through the use of this particular object, you can actually get the server to find out the current time and display that time in the time field using the AJAX XMLHttpRequest.
The one exception to this rule would be Internet Explorer, since Microsoft always has to be different from everyone else! Microsoft, instead of using the XMLHttpRequest, actually uses an ActiveXObject, which basically means that when you are programming in AJAX you need to account for both Internet Explorer possibilities as well as possibilities for other browsers like Firefox, Opera, Safari and many others as well. Internet Explorer, however, is the only browser that does not use XMLHttpRequest, so as long as you have accounted for Explorer you are not going to have to worry about any of the other browsers.
The way to create these contingencies is through the use of what is known as a try and catch statement. You will first set up the XMLHttpRequest and then the ActiveXObject in case the catch is not successful in the first case. Every now and then, it is possible that you will encounter someone with a browser that is years and years out of date. If this is something that you think is likely to happen on your website, you might want to program another catch into the AJAX code that displays something on the blank screen letting the user know that their browser is not compatible with AJAX; however, that is entirely your choice.
Scriptycan is a great software repository featuring both free and commercial Ajax Scripts and applications for developers and programmers.