This tutorial is intended to be a first look at the concept of “Ajaxifying” your website. That is, creating a progressive-enhancement to how users navigate from page to page. Traditionally when a user clicks a link, the new page in it’s entirety will be loaded. If your header / navigation / footer stays the same throughout, there is little need to re-download that content, furthermore to re-render that content after it downloaded.
This is a generic pattern that you can expand on and implement with your web application.
First the Javascript
-
-
var QuickLink = Class.create({
-
-
currentPage : null,
-
-
initialize : function() {
-
this.attachLinkEvents();
-
//Set an interval to check for changes in the URI
-
setInterval(function(){
-
this.refresh();
-
}.bind(this), 1000);
-
},
-
-
refresh : function() {
-
//If a change has taken place, call the dispatch function
-
var url = window.location.hash.split(‘#’);
-
if (url.length > 1) {
-
this.dispatch(url[1]);
-
}
-
},
-
-
attachLinkEvents : function() {
-
//Gather a list of links, and replace the default action
-
//With our javascript action
-
var links = $$(‘a’);
-
links.each(function(linkElement, index) {
-
var href = linkElement.href;
-
if(href.indexOf(‘http://www.yourdomain.com/’) != -1) {
-
var uri = href.split(‘http://www.yourdomain.com’)[1];
-
Event.observe(linkElement, ‘click’, function(e){
-
window.location.hash = uri;
-
//Disallow the default click to change pages
-
Event.stop(e);
-
});
-
}
-
-
});
-
},
-
-
dispatch : function(url) {
-
//Check to see if the page you are linking to is NOT the current URL
-
//If different, then send your request
-
if (this.currentPage != url) {
-
this.currentPage = url;
-
var myAjax = new Ajax.Updater(‘content’, url, {
-
parameters : { ‘quick’ : true }
-
});
-
}
-
-
-
}
-
});
-
-
//initialize
-
-
new QuickLink();
-
-
Your template
-
-
<? if ($_POST[‘quick’]!=true) { ?>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<head>
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
-
<title>Page Title</title>
-
</head>
-
<body>
-
<div id="content">
-
<? } ?>
-
Some content here
-
<? if ($_POST[‘quick’]!=true) { ?>
-
</div>
-
</body>
-
</html>
-
<? } ?>
-
In conclusion
What we are doing is using AJAX just to refresh the content area. Your template checks to see if it was an AJAX request and if so, does not send the header and footer of your content back. This saves you in the following areas:
- Does not have to re-download everything
- If you have scripts that need to be initialized, the scripts will not need to be reparsed
Moving further
If using a more advanced setup like an MVC framework or if your app has different scripts or elements depending on the page I suggest on the server side you instead send back a payload including dependent scripts and maybe flags to highlight elements as well. Then you can use output buffering to store the HTML chunk in part of the payload. Return the response as a JSON object have your Javascript put things where they need to go and THEN write the innerHTML of the content part of the payload.
