I have developed a small Javascript tool to enable partial content loading in a Plone site using AJAX (the global AjaxNav
object contains all the necessary functions). I want the browser history (back and forward buttons) to work correctly, so I wrote the following function:
var set_base_url = function (url) {
var head=null,
base=$('html base').first();
if (base) {
base.attr('href', url);
log('set base[href] to '+url);
} else {
$(document).add('<base>').attr('href', url);
log('created <base> and set href to '+url);
}
var history_and_title = function (url, title, data) {
var stateObj = {
cnt: increased_state_counter()
};
if (typeof data.uid !== 'undefined') {
stateObj['uid'] = data.uid;
}
if (title) {
if (AjaxNav.options.development_mode) {
title = '(AJAX) ' + title;
}
document.title = title;
}
if (url) {
set_base_url(url);
window.history.pushState(stateObj, '', url);
} else {
window.history.pushState(stateObj, '');
}
};
AjaxNav.history_and_title = history_and_title;
Despite calling this function without any visible errors (at least that I could see), clicking the "back" button in the browser or using the backspace
key only changes the URL displayed, without reloading any content. While I am willing to accept full-page reloads for now, I believe it would be preferable to reload pages from the history via AJAX.
Can anyone spot an obvious error?
The code can be accessed at this link, although it is quite lengthy (~850 lines) because of the ambiguity of target URLs, which may specify an object or its view method. Therefore, I attempt up to two different URIs per hyperlink before processing (replacing contents, setting the title, event.preventDefault(), etc.), or returning true
to load the entire page.