Book Section
TY JavaScript 3rd Ed.
Teach Yourself JS 1.5
Teach Yourself DHTML
Teach Yourself JS 1.3
LLWW: JavaScript

General Section
Discussion Forum
Articles / Tips
JavaScript Links
About the Author
Privacy Policy
Contact Me

Other Sites
Website Workshop
JavaScript Weblog

JavaScript Workshop Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
When to use AJAX

Post new topic   Reply to topic    JSWorkshop Forum Index -> Articles
View previous topic :: View next topic  
Author Message
Senior Member
Senior Member

Joined: 17 Jul 2002
Posts: 921
Location: UK

PostPosted: Thu Aug 03, 2006 4:57 am    Post subject: When to use AJAX Reply with quote

I don't know about you, but I find a squirt of AJAX keeps the cats out of the garden. (This joke may only work in the UK - apologies if this is the case.)

However, the more important question, "When to use AJAX", relates to Asynchronous JavaScript And Xml.

You see, it's this simple - AJAX is incredibly powerful and allows your page to become a hive of activity, changing fluidly between a series of views based on user input without posting back to the server.

If you don't know what AJAX is or how to use it, check out Michael's latest book "SAMA Teach Yourself JavaScript in 24 Hours, Fourth Edition".

If you do know about it and are thinking of using it - read on...

Part One - What's it For?

You could live your whole life without AJAX, there isn't a big hole waiting to be filled by this technology - so why use it?

Well, while it doesn't add things we couldn't do before, it does completely change the way we do them. On a basic level, you never have to leave the page you're on when you use AJAX, it happily goes off to get more information in the background and supplies it back to the original page to use as it wished. While this is no particular help for normal page-to-page movement on the internet, it does suddenly open up some interesting new possibilities.

As always, Google are one of the first big names to capture the power of AJAX. Check out (UK) or (US) and have a play. You can zoom in on maps / satellite images and find directions - all stuff we've done before on various map sites. However, one thing you can do with Google Maps is click on the map and drag it. If you have a few spare moments, you can just sit back and indulge in an armchair exploration of your whole country.

Now if this was a traditional page, you would have to load up the images for your whole country in order to do this - but with AJAX, it just pops off and finds out what images it needs to fulfil your dragging request - expanding your map as you demand more bits. This means you get an initial page load when you arrive on your map and then lots of little page loads as you move around the map.

Now isn't that better than clicking on a "North" button and waiting for another page?

Another popular use for AJAX is to turn web mail into a slicker experience, removing all those page loads required to look through your email and replacing it with a simple list and AJAX view that goes off and gets the content for an email when you click the subject line.

It sounds great doesn't it - and it is. It adds some powerful background communication to the server that allows you to enrich your web application. Now there can't be any good reason not to use AJAX can there?

Part Two - When NOT to use it!

Here are a few pointers to bear in mind when you're heading off down the AJAX pathway that will help you to avoid some common pit falls.

The main thing to bear in mind is, will this page still be usable without AJAX. For example, if you've used Google Suggest (which hints at what you might be about to type as you enter your search) you can still imagine using the page without the AJAX working. You need to be able to imagine YOUR page still working without AJAX.

Now, I'm not about to say that this is a golden rule - it's probably a silver one - if you imagine the AJAX bit not working on the Google Maps site I mentioned earlier, it wouldn't be worth anything if you visited the page and there wasn't a map - so you have to make sure that the page can either work fine without it or has something useful in the NOSCRIPT tag so your users and visitors still get something. This is because JavaScript could be disabled, the user may be using a specialist browser (for example, a reader for the partially sighted) or you could be getting read by a web-bot that wants to index your page.

On this basis, try to keep things like navigation and full-on content areas AJAX free as much as possible on public web pages.

Basic Theory:

Here is the "SOHNEE" basic theory on whether you should use AJAX.

1. Does it make the site better / easier to use (if YES - proceed!)
2. Am I attaching AJAX to a "click" event (if YES - think very hard before you proceed - this is probably the worst event to attach AJAX to as it essentially breaks the users "back" button and won't be indexed properly by web-bots. It also means that the page won't look like it does now if the user adds the page to their favourites or emails the URL to a friend - it's not a definite no-go, but make sure you've thought about it before you crack on...)
3. Can the user live without the information from the AJAX call if nothing ever came back?
- YES - you may still wish to provide the information in another way using the NOSCRIPT tag or by using a post back
- NO - you need to work out your alternative strategy for getting the information to the user in the event of a failure, disabled JavaScript or usability issue. Include NOSCRIPT tags, have an alternative post-back method or come up with another way of getting you information out there. The only time you can be excused from this is if you can force your users down a certain road. For example, it's likely that most blind people aren't interested in browsing Google Maps and it's likely that you can force people into turning JavaScript on in order to use a web mail application.

Part Three - Conclusion

Essentially, AJAX is a powerful technology - but don't use it just for the sake of it, use it because it does something useful.

I'm sure we can all remember the "Flash" revolution. There are some great sites that use flash to deliver something that you otherwise couldn't get - but equally, there are sites that were written in Flash for Flash's sake and did nothing that a HTML page couldn't have done.

Don't make the same mistakes this time round!
I work for:
Unsigned and Independent Music Magazine
Back to top
View user's profile Send private message Visit poster's website

Joined: 07 Sep 2002
Posts: 103
Location: Earth

PostPosted: Sat Aug 19, 2006 11:17 am    Post subject: Reply with quote

nicely said
Firefox Thunderbird
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    JSWorkshop Forum Index -> Articles All times are GMT - 7 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2002 phpBB Group
(c) 1997-2002 Starling Technologies and Michael Moncur. Portions (c) Sams Publishing.