When to Use Ajax and When Not To

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

140bytes @ hhjs
TR4NSLATOR/Flikr/CC BY 2.0

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.

    Things That Are Good for Ajax

    • Form validation
      This is almost a no-brainer. It's so much nicer when the form tells you as you are typing if you've filled it out wrong or not. Having to go to the server and then return an error message is not only old, it's slow. Leave the server validation in the form, that's important for accessibility. But for those who can support Ajax, tell them right away.
    • Comments
      Comments on blogs or even just articles are a great use of Ajax. Comments can change all the time, and especially when a commenter hits the comment button, it's nice to see the comment appear immediately on the page.
    • Filtering data
      If you've got a large table with a lot of data in it, a nice application for Ajax is to add filters and sorters to the table. Getting your Web table to act more like Excel is really useful to people.
    • Surveys and polls
      When you click on your vote, the poll would just switch to show you the results. And before you comment, About doesn't yet support Ajax on our polls - but it sure would be nice. Maybe we can give the About.com developers an "Ajax call" of our own. :)

    What to Do When You Get the "Ajax Call"

    Talk to your boss or marketing department to find out why they want to use Ajax on the Web site. Once you understand the reasons for why they want it, you can then work on finding a suitable application for it.

    Remind both your boss that your customers come first, and that accessibility is not just a word. If they don't care whether your site is accessible to customers, then remind them that search engines don't care for Ajax, so they won't get as many pageviews.

    Start small. Build a something easy first before worrying about building an entire new Web application from scratch. If you can get something Ajaxian onto your Web site, that might be all your boss or marketing department needs to meet their goals. It is definitely possible to put up an Ajax application that is actually useful, but only if you think about how to do it first.

    Did you find this article useful? Make a comment.