Often it’s because someone wants to use words like “AJAX”, “JSON”, “dynamic”, and “Web 2.0” in marking materials. Sometimes it’s because people believe that scripts are somehow more maintainable than the code behind. The rest of the time it’s just pure laziness.
Thoughts on AJAX and JSON
AJAX and JSON are very powerful tools in the web developers’ arsenal, they give you the power to send and pull data from servers without forcing a page load. However, it is very easy to take it too far and break the golden rules. These technologies are best used on top of existing (working) web applications that only require a 1-step postback.
Every web developer knows of AJAX, it is synonymous with dynamic pages. AJAX is used to send standard GET or POST requests to web servers through the browser and pull XML data back. This offers almost limitless options for a web developer. You can literally build and run an entire web application on top of one HTML page.
For a good example of AJAX and JSON taken too far, just take a look at Twitter, specifically their search. Every time you see a # symbol in the URL you are seeing a hack to support dynamic data and the back button. I understand why they did it, to save page loads, and because it is easier on the servers to publish XML/JSON than it is to craft and return an HTML layout. But they could have accomplished the same thing by publishing the XML data to the browser with an XSLT stylesheet for look and feel.
The World of Warcraft web site uses the XML/XSLT technique and it works great. Since Twitter’s whole service, API, and back-end is based on publishing XML data this technique would have been the perfect solution for them. All of the fancy dynamic loading only reduces the usability of the search feature.
Remember Twitter doesn’t have to worry about SEO, accessibility or backwards compatibility. You do. Don’t use AJAX or JSON for anything but gloss and finish on a functioning base web site. Relying on dynamic calls for widgets and decorations are fine, but don’t require it for any real content.
I will add that I think all of the AJAX in gmail is fine. Why? Because gmail is a web application, behind a login screen. The context is different than a page on a front-facing web site that you might find through a search engine. Web users understand that they are entering something different than a static page when they log in to their gmail accounts.
Thoughts on libraries
Forcing users to download 81kB of script just so you can
$('a#next').click() is a complete waste. You can accomplish the same thing just as easily with the native DOM functions and skip the extra bandwidth, server load, and page load times.
However, if you know that you’re going to be doing a lot of advanced scripting on a site, then please, use a library, preferably jQuery. Having one browser-cached file with all of the fundamental work done can be a lifesaver. jQuery is very powerful and can save you lots of time and headaches on a big project.
Updated: Apr 27th, 2011