I have created a web app which utilizes the history
pushState
and replaceState
methods in order to navigate through pages while also updating the history.
The script itself works almost perfectly; it will load pages correctly, and throw page errors when they need to be thrown. However, I've noticed a strange problem where pushState
will push multiple, duplicate entries (and replace entries before it) to the history.
For example, let's say I do the following (in order):
Load up index.php (history will be: Index)
Navigate to profile.php (history will be: Profile, Index)
Navigate to search.php (history will be: Search, Search, Index)
Navigate to dashboard.php
Then finally, this is what will come up in my history (in order of most recent to oldest):
Dashboard
Dashboard
Dashboard
Search
Index
The problem with this is that when a user clicks the forward or back buttons, they will either get redirected to the incorrect page, or have to click multiple times in order to go back once. That, and it'll make no sense if they go and check their history.
This is what I have so far:
var Traveller = function(){
this._initialised = false;
this._pageData = null;
this._pageRequest = null;
this._history = [];
this._currentPath = null;
this.abort = function(){
if(this._pageRequest){
this._pageRequest.abort();
}
};
// initialise traveller (call replaceState on load instead of pushState)
return this.init();
};
/*1*/Traveller.prototype.init = function(){
// get full pathname and request the relevant page to load up
this._initialLoadPath = (window.location.pathname + window.location.search);
this.send(this._initialLoadPath);
};
/*2*/Traveller.prototype.send = function(path){
this._currentPath = path.replace(/^\/+|\/+$/g, "");
// abort any running requests to prevent multiple
// pages from being loaded into the DOM
this.abort();
return this._pageRequest = _ajax({
url: path,
dataType: "json",
success: function(response){
// render the page to the dom using the json data returned
// (this part has been skipped in the render method as it
// doesn't involve manipulating the history object at all
window.Traveller.render(response);
}
});
};
/*3*/Traveller.prototype.render = function(data){
this._pageData = data;
this.updateHistory();
};
/*4*/Traveller.prototype.updateHistory = function(){
/* example _pageData would be:
{
"page": {
"title": "This is a title",
"styles": [ "stylea.css", "styleb.css" ],
"scripts": [ "scripta.js", "scriptb.js" ]
}
}
*/
var state = this._pageData;
if(!this._initialised){
window.history.replaceState(state, state.title, "/" + this._currentPath);
this._initialised = true;
} else {
window.history.pushState(state, state.title, "/" + this._currentPath);
}
document.title = state.title;
};
Traveller.prototype.redirect = function(href){
this.send(href);
};
// initialise traveller
window.Traveller = new Traveller();
document.addEventListener("click", function(event){
if(event.target.tagName === "a"){
var link = event.target;
if(link.target !== "_blank" && link.href !== "#"){
event.preventDefault();
// example link would be /profile.php
window.Traveller.redirect(link.href);
}
}
});
All help is appreciated,
Cheers.