Skip to content

Instantly share code, notes, and snippets.

@kbariotis
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbariotis/9082048 to your computer and use it in GitHub Desktop.
Save kbariotis/9082048 to your computer and use it in GitHub Desktop.
AncientJS is a minimal History API implementation.
var ancientJS = {
_bindUIEvents: function() {
$("body").on("click", "a", this._linkPushed);
$(window).on("popstate", this._onPopState);
},
init: function() {
this._bindUIEvents();
},
_linkPushed: function(e) {
if (!!window.history &&
typeof(window.history.pushState) == 'function'){
e.preventDefault();
ancientJS._pushState(
{
url: window.location.href
},
window.location.href,
$(this).attr("href")
);
} else {
window.location = $(this).attr("href");
}
},
_goTo: function(url, state) {
$.get(url, function(data){
$("#blog").html(data);
document.title = $("#blog h1").text();
})
},
_pushState: function(data, title, url) {
history.pushState(data, title, url);
ancientJS._goTo(window.location.href, data);
},
_onPopState: function(e) {
if(!e.originalEvent.state){
history.replaceState("NULL", document.title, window.location.href);
return;
}
ancientJS._goTo(window.location.href, e.originalEvent.state);
}
};
ancientJS.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment