kettek/javascript/history.js

44 lines
1.7 KiB
JavaScript

/* This script implements HTML5 History states for clients that support it. It parses all links for those beginning in "?"(local), replaces with History-browsing functionality, and onclick updates the "content" div (and reparses accordingly). */
var hist = {
content: null,
Go: function() {
if (window.history.replaceState === undefined) return;
this.content = document.getElementById("content");
window.history.replaceState({content: this.content.innerHTML, url: window.location.href, title: document.title}, document.title, window.location.href);
this.doParse();
window.onpopstate = this.popState;
},
doParse: function(ele) {
var l = (typeof ele === 'undefined' ? document.getElementsByTagName('A') : ele.getElementsByTagName('A'));
for (var i = 0; i < l.length; i++) {
if (!l[i].href) continue;
if ((l[i].getAttribute("href")).charAt(0) != '?') continue;
l[i].onclick = function() {
hist.loadContent(this.getAttribute("href"));
return false;
};
}
img2popup.doParse(hist.content);
},
loadContent: function(target) {
var r = new XMLHttpRequest();
r.open("GET", "page.php"+target, true);
r.onreadystatechange = (function(r, target) { return function() {
switch (r.readyState) {
case 4:
hist.content.innerHTML = r.responseText;
window.history.pushState({content: r.responseText, url: target, title: document.title}, target, target);
hist.doParse(hist.content);
break;
}
}})(r, target);
r.send();
},
popState: function(e) {
if (e.state == null) return;
hist.content.innerHTML = e.state.content;
document.title = e.state.title;
hist.doParse(hist.content);
}
};