I am trying out the history api for changing the url after swapping out content and have run into an issue with stopping the a tags from loading a new page. So far, when you click a button, it loads the requested content within the page but then loads the page where the requested content is stored. Any suggestions about how to stop this and get a result of a webpage with new content and a fresh url that can be shared?
Here's the JS.
function supports_history_api() {
return !!(window.history && history.pushState);
}
function swapText(href) {
var req = new XMLHttpRequest();
req.open("GET",
"http://bonkmas.com/historytest/gallery/" +
href.split("/").pop(),
false);
req.send(null);
if (req.status == 200) {
document.getElementById("open-content").innerHTML = req.responseText;
setupHistoryClicks();
return true;
}
return false;
}
function addClicker(link) {
link.addEventListener("click", function(e) {
if (swapText(link.href)) {
history.pushState(null, null, link.href);
e.preventDefault();
}
}, true);
}
function setupHistoryClicks() {
addClicker(document.getElementById("next-vid"));
addClicker(document.getElementById("previous-vid"));
}
window.onload = function() {
if (!supports_history_api()) { return; }
setupHistoryClicks();
window.setTimeout(function() {
window.addEventListener("popstate", function(e) {
swapText(location.pathname);
}, false);
}, 1);
}