Javascript handler for links
<script type="text/javascript">
var APP = {
"ajaxGet" : function(loc, dir) {
$.ajax({
url : loc,
type : "GET",
data : "",
dataType: "json",
error : function() { alert("Error loading html document"); },
success : function(res) {
// Update appropriate web page portions
APP.updatePage(res);
// Update analytics to track usage
APP.updateAnalytics(loc);
/*
Push loc in history stack to enable browser back button,
but do it only if the dir (direction) is not "back"
otherwise history will get filled with "previous" location
thereby setting a never-ending loop.
*/
if (dir !== "back") {
history.pushState(null, null, loc);
}
}
});
},
"updatePage" : function() {
},
"updateAnalytics" : function() {
};
$(document).ready(function() {
/*
Enable each link to fire the ajaxGet() function
on being clicked. Send an empty string for "dir".
*/
$("a").click(function() {
APP.ajaxGet($(this).attr("href"), "");
/*
returning false ensures that the href
in the anchor is not fired, and instead,
ajaxGet() is used
*/
return false;
});
/*
The following bit of code enables the browser back button.
When the user clicks the back button, the location from
the history stack is fed to ajaxGet() to restore the previous
web page.
*/
window.setTimeout(function() {
window.addEventListener(
"popstate",
function(e) {
APP.ajaxGet(location.href, "back");
},
false
);
}, 1);
});
</script>