Should you use AJAX?
This is a question that is on top of everyone’s mind: “Should I use AJAX?” or another question is: “How do I convince my boss that we need to look into AJAX?” Well the basic idea is there are times to use it and there are times that you should not use it! There are benefits and there are drawbacks too just like any other technology out there.
Before I discuss the drawbacks and benefits, let me explain the classic Web site model versus the AJAX model in simple non-technical terms that drivers can relate too. (Well most of you may consider this non-technical)
The Car Analogy
The classic Web page model is like a manual car with a bad driver. First we know we need to get to our destination so we start up our car and start driving. We shift through the gears and get to a stop sign. We step on the brake without stepping on the clutch so we stall. So we have to restart the car. We get to the next stop sign and we forget the clutch again and we stall. Again we need to restart the car. We finally get to our destination where we can turn off the car.
Now the AJAX model is like an automatic car. We start up the car and start to head to our destination. We stop at the first stop sign look both ways and we release the break. We keep driving and we do not worry about stalling since we are in an automatic (Well in my old car I had to worry, but that is a different story.) Again we stop and we are able to continue without turning the key. We get to our destination without causing the cars behind us to honk, since we did not stall and have to wait to restart!
Now in this analogy we have two stops signs. These stop signs could have been form verifications, double combo scripts, data retrieval, data posting, and so on. AJAX is able to do all of this without causing the whole entire page to be stopped and started. This stopping and starting can be seen as the stalling of our manual car. After a time it gets really annoying since we have to worry about the current page state. Will it remember the scroll position on the page, will my forms be filled in, will the information be cached or am I pulling all of the data? With AJAX, we are able to eliminate all of those questions/concerns a developer may have.
Benefit and Drawbacks
One benefit that I see is this. You have a double combination script on your web page and your first selection list is dynamically built on the server. You may have to rebuild that selection list every time the page is rendered and select any value that is there. That is a waste of the processor in my mind. With AJAX you would only pass back the value and obtain the new selection values. That means we are only grabbing the data we need and not having to process all of this excess. We do not have to wait as long as a classic post back. AJAX is not as quick as a desktop application since we do have a slight network lag, but it is a lot quicker than having to wait for the page to re-render!
Now what are the drawbacks to Ajax? Can your server handle all of the requests with certain Ajax applications? For example, a type-ahead script that posts back to get the newest results can have multiple requests. Imagine 1000 people using it with large words. How many requests is that?
The famous problem with frames effects AJAX too: Bookmarking a page. Do a search on Google and you get that handy dandy query string which you can send to other people. Do a search that utilizes Ajax and you loose that functionality! How can you overcome this? Incorporate a link on the page that a user can copy and paste. Add either client side or server side processing that checks for this link and returns the results. Little more work, but at least we can see the results.
The third drawback is not a drawback for me, but one for people that are afraid of a technology that is so much fun to play with. This would be JavaScript. Just like I said in my blog yesterday, people are afraid of JavaScript since it has cross-browser compliance issues. That is why it may be a drawback. With a little effort, you will see this is not a drawback. In Ajax In Action and this blog, I try to help you understand the issues with Cross Browser code and how to overcome the common problems. One of the downfalls to JavaScript is the code examples online are wrong on some big sites since the code is out dated and not updated. It is equivalent of using the first Beta version of Java or .NET and today’s current version. Things change!
Now back to our question from the start
Now is AJAX for you? The answer is really for you and your company to decide. Can you take the hits on the server for certain application types? Can you improve the user’s experience? Can you write the JavaScript? Can you understand the packages/libraries that are out there that are available to download? Do you want to save processor space? Do you want to use a cool technology and impress your friends at dinner? (Wait that should not be a reason why you use it!)
I know you want to see some sample code, but I still want to explain other issues I see before I even let you get your hands dirty!
Eric Pascarello
Moderator of HTML/JavaScript at www.JavaRanch.com
Author of: JavaScript: Your Visual Blueprint for Dynamic Web Pages