1. Technology

When to Use Ajax and When Not To

What to Do When You Get the "Ajax Call" from Your Boss


140bytes @ hhjs

I admit it, I've never been a huge fan of JavaScript. I was always really glad that About had a JavaScript Guide so that I didn't have to cover it on my site. I can read and write JavaScript, but until lately, I had very little interest in it. For whatever reason, my mind had a complete mental break when it came to writing JS scripts. I can write complicated C++ and Java applications and I can write Perl CGI scripts in my sleep, but JavaScript was always a struggle.

Ajax Made JavaScript More Fun

I think part of the reason I didn't like JavaScript was because rollovers are boring. Sure, you can do more than that with JS, but 90% of the sites out there using it were doing either rollovers or form validation, and not much else. And once you've validated one form, you've validated them all.

Then Ajax came along and made it all new again. Suddenly we had browsers that would support JavaScript doing something other than swaping images and we had XML and the DOM to connect data to our scripts. And all of this means that Ajax is intersting to me, so I want to build Ajax applications.

What's the Stupidest Ajax Application You've Ever Built?

I think mine would have to be the email checker on an account that got almost no email. You would go to the Web page and it would say "You have 0 mail messages." The 0 would change if a message came in, but since that account got no mail, it would never change. I tested it by sending mail to the account, and it worked. But it was absolutely pointless. There were better mail checkers available five years ago, and I didn't have to have Firefox or IE running to use them. When one of my co-workers saw it she said "What's it do?" When I explained, she asked "Why?"

Before Building an Ajax Application Always Ask Why

Why Ajax?
If the only reason you're building the application in Ajax is because "Ajax is cool" or "my boss told me to use Ajax," then you should seriously evaluate your technology choice. When you're building any Web application you should be thinking of your customers first. What do they need this application to do? What will make it easier to use?

Why Not Something Else?
It can be very tempting to use Ajax simply because you can. On one site that my team was working on, there was a tabbed section of the page. All the content was stored in XML in a database and when you clicked on the tabs, Ajax was used to rebuild the page with the new tab data from the XML.

This seemed like a good use of Ajax, until you start thinking of some of the issues with it:

  • The tabs cannot be bookmarked. So customers can't save the information they want.
  • Search engines don't see the data that isn't in the first tab, because they can't access the Ajax.
  • Ajax is not accessible, so the content in the other tabs would not be visible to anyone using a screen reader, or even older browsers that don't have good JavaScript support.
  • If one of the tabs had a lot of information, it could take a long time to load on a slow connection. And because Ajax doesn't indicate anything is happening it looks like the page is broken.

The thing that was interesting, is that this Web site had similar pages in the past that didn't use Ajax. They delivered the content either with hidden divs or separate HTML pages. There was no reason to use Ajax other than that Ajax was cool, and our boss had suggested we look for places to use it.

Ajax is for Action Not Content

If you're going to put up an Ajax application, or just something Ajax-like on your Web site, first determine if the data you're accessing changes. The point of the asynchronous request is that it makes requests to the server for information that has changed faster - because it's happening while the reader is doing something else. Then when they click a link or button (or after a set amount of time - whatever your distinction is) the data shows up right away.

If your content or data never changes, then you shouldn't use Ajax to access it.

If your content or data only rarely changes, then you probably shouldn't use Ajax to access it.

Page 2: Things that are good for Ajax, and what to do when you get the call.

©2014 About.com. All rights reserved.