AJAX Thoughts, Suggestions and Examples Part I

AJAX, previously know as a household cleaner, now is a quite common term used for web sites. We won't go into what AJAX is or the basic implementation. This is covered in multitues of articles online already. What I want to point out are ideas for smart implementation and browser compatibility. I have seen some poor implementations from so called "professional" programmers. Not saying that my ideas and/or code are perfect, I'm sure they could be better, but they have been tested to show support for common browsers such as IE6+, Firefox 0.8+, Netscape 7.0+ and Safari for Mac. They should also work for Opera 6.0+ and possibly other browsers.

Use AJAX when appropriate, not for everything. This may seem obvious, but I have seen people go AJAX crazy which instead of making for a more pleasurable web experience, degrades it. Let's take an example, we have a server side web page which has dynamic links based on the user's rights and two select lists also with dynamic list contents based on their rights. The web page loads, then multiple synchronous AJAX requests are sent to the web server to set up the initial web page data. This includes which links are available to the user, the items available in the select lists, even the rights the user has on the page.

In this scenario, AJAX has done absolutely nothing to improve the web experience, it has actually degraded it. Instead of waiting for the server to process a web page and send it to the client (this includes server processing for rights determination, rendering the correct links on the page and rendering the dynamic select lists) with a single request to the server, now five web requests have to be made to the server. In a LAN environment, this may be acceptable since latency is not much concern, but over a WAN connection, the combined latency for multiple web requests can become unnecessarily slow.

One touted benefit of AJAX is transferring some load from the server to the client, but in this situation the server still needs to do about the same amount of work, if not more. It still needs to process rights determination, determine the correct links for the user and determine the correct items for the select lists. The only things it doesn't have to perform is the rendering of the web page to reflect the dynamic content, however this should surely be counteracted by the lookup and processing of session information which will be required for each AJAX request.

So the moral of Part I is that for any data that the page will display which is static for the user should be loaded directly into the page, not be gotten from subsequent AJAX requests. Notice the key, "static for the user". This means the content itself can still by dynamic, but if it does not need to be changed while the user is viewing the page, it is most likely best rendered by the server.

Explore more in AJAX Thoughts, Suggestions and Examples Part II here