Written by nitinpai on April 10th, 2007
Optimal use of Ajax – use Ajax the smart way
Web 2.0 is the thing that can make your site stand out from the hoard of web sites springing up day by day. But are they efficient enough to make a big impression? You need to analyze on some common principles to make the best impression. These principles make you use it in the smart way! Web 2.0 today dominates mostly in the usage of Ajax today. I had written on how to go about choosing the right framework and the different types of frameworks which are available to you to join the web 2.0 league. Today let’s see how can you can use Web 2.0 features in the smart manner because the smartness will inspire others to work efficiently as well.
Looking at the features of Ajax that hundreds of frameworks today provide, you might get sometimes tempted to use them all. Its pleasing when the user interface is more interactive and more user friendly at the same time attractive. To make these you have to use the framework which provides such eye candy. But before you decide on a particular you must have these things in mind.
- Check for frameworks or API for memory leaks
- Don’t bloat your code
- Use interactivity only where required
- Don’t try to use Ajax where bookmarking is heavily used
- Test your Ajax code properly
Let’s see each one in detail.
2. Check for frameworks or API for memory leaks
If you had seen the DOJO toolkit site earlier which works on DOJO you might have sometimes cursed the loading time for the site. Many a times my browser has crashed the moment I opened the site. This is because frameworks like DOJO implement the Ajax behavior and other features using a lot of code. And hence when the site loads the features use up the system memory for rendering the widgets in the browser.
3. Don’t bloat your code
4. Use interactivity only where required
Never think in the manner that since your site is having the web 2.0 feature each and everything in it should be made via scripts. This is bad thinking. Ajax promotes interactivity hence it is must that you just make the page interactive when there are many components on the page. If you have personalized the Google homepage you might probably know about this very well. In the personal homepage you can see how convenient it is to modify a single component without disturbing the others. So Ajax fits in the picture in such a case.
Now lets take the example of a blog. On the homepage of a blog you see a list of posts and when you click the title of a post, the post page loads up. Now whether you simply load the page in the normal fashion or if Ajax is used in here, the time taken is the same. No fast loading takes place nor nothing on the site looks more appealing if Ajax is used in such situation since the data to be fetched from the server does not change.
The point I am trying to make is that Ajax should be used in those places where it solves some purpose and not in the places where it seems unnecessary. It is just a waste of time to put the efforts in this case.
5. Don’t try to use Ajax where bookmarking is heavily used.
If you are a blog owner then you might understand the importance of trackbacks or permalinks. Those are the blog’s savior since they are universally unique and reachable to any person who uses it whenever required. Ajax has a very undesirable feature which might make a blog less popular i.e Ajax pages cannot be bookmarked. If you use Ajax to load a post as I had mentioned in the example of point number 4. then the post loads up the same page without changing the URL. This is because it is loaded asynchronously.
Now if a person looks at such a loaded page and likes the content he might immediately bookmark without a second thought. But when he requests for the same page again.. voila! he doesn’t see the post. Instead he sees your homepage. Of course you can avoid this by explicitly specifying the trackback URL but you can never gaurantee that the reader of your content might be even knowledgeable of what Ajax is and what trackbacks are used for. Hence your blog traffic might suffer.
6. Test your Ajax code properly