Use Event Delegation
The basic key to AJAX-powered sites is using event delegation. Since you should never assume a given element exists in the page (...because it may have been brought in via AJAX), you should never try to directly assign event listeners to elements (other than the
document.body
). Instead, the best course of action is to use event delegation. Here's a very basic example of how you'd use event delegation with jQuery:
During the creation of my site, I coded my JavaScript as though no element other than the
<body>
existed. As a result, it took me less than 30 minutes to implement AJAX page navigation. Event delegation makes AJAX page loads possible.Use Pub/Sub to Signal Events
Event delegation signals when a link has been clicked, but implementing a publish & subscribe system provides much more power to your app. If you aren't familiar with pub/sub, think of it in terms of how radios work: radio stations publish (music) without needing to know who is listening. Listeners can subscribe (listen) without having a relationship to the station.
I'll use an example from my site with regard to why and how you'd use pub/sub. For any given article, I will want to (1) load the popular social media widgets and (2) show a star rating widget so users can tell me how useful the post was. On a more static site, you'd simply execute code to create those widgets during
DomContentLoaded
. On an AJAX-heavy site, however, we may need to execute this functionality several times so we'll need a signal for when the page changes and we'll need to recreate these widgets.
Here's how we'd use pub/sub to accomplish this scenario:
What's great about pub/sub is that we get to keep our JavaScript modules decoupled from the AJAX piece, and we don't have to deal with massive callbacks. One
publish
call and we're letting any module subscribe and react to that event!Use a Solid pushState Manager
AJAX-heavy sites don't simply want to load new content -- they should also reflect the new page URL using theHTML5 History API. David Walsh Blog, a MooTools-based site, uses Christoph Pojer's outstanding History plugin. A popular jQuery utility for history is pjax.
Ensuring the browser history stays in sync with the AJAX page loads is important so that users can refresh, bookmark, or share the current page URL accurately. Doing so also ensure the browser's back and forward mechanisms work properly!
Place a Notifier in a Consistent Location
One potential problem, confusing users because they don't see the browser's loading bar moving, can be nipped in the bud by placing a notifier (otherwise called a spinner) in a consistent location on the page. It's important that users are always aware of when processing is occurring, and this is most true when dynamically loading and rendering page content!
Cache Request Results by URL
With the web, you never want to make more requests than you need to, and that's especially true when you're making full page requests. Creating a caching system for an AJAX-powered site is (usually) much simpler than you think:
This cache will not only display once a link is clicked multiple times, but when the user triggers back and forward actions. Your server and users will thank you for the client side caching!
Know When Not to Use AJAX
Not every page should be loaded via AJAX, and it's important that developers can identify cases when AJAX would be problematic. In many cases, the results of form submissions shouldn't be cached or fetched dynamically. There may be scenarios where your infrastructure would suffer from AJAX page loads. If you can identify URLs which shouldn't be loaded via AJAX, you can modify your jQuery event delegation selector to accommodate for them:
You can also add more logic within the event delegation callback to accommodate for more URLs; if the URL clicked is a match, you can forego the
preventDefault()
call and simply let the page load per normal.
When you plan ahead and code flexibly, creating an AJAX-powered website is probably easier than you think. Keep in mind the tips provided in this post and you're 80% done. Of course, you will find a few niggles specific to your site, but those will be minor in relation to the overall app. Happy AJAXing!