Fix anchors popstate bug

This commit is contained in:
Tissevert 2019-02-25 22:46:08 +01:00
parent 4368f30531
commit 2c29b331b8
1 changed files with 14 additions and 4 deletions

View File

@ -14,7 +14,11 @@ function Navigation(modules) {
}
);
});
window.addEventListener('popstate', function(e) {navigate(e.state.url);});
window.addEventListener('popstate', function(e) {
if(e.state != undefined) {
navigate(e.state.url);
}
});
history.replaceState({url: window.location.pathname}, 'Blog - title', window.location.pathname);
return {
hijackLinks: hijackLinks
@ -25,7 +29,8 @@ function Navigation(modules) {
var links = domElem.getElementsByTagName('a');
for(var i = 0; i < links.length; i++) {
var a = links[i];
if(a.classList.contains("navigation")) {
var href = a.getAttribute("href");
if(href[0] == "/" || href[0] == "#") {
a.addEventListener('click', visit(a.getAttribute("href")));
}
}
@ -34,8 +39,13 @@ function Navigation(modules) {
function visit(url) {
return function(e) {
e.preventDefault();
history.pushState({url: url}, 'Blog - title', url);
navigate(url);
if(url[0] == '#') {
window.location = url;
history.replaceState({url: window.location.pathname}, 'Blog - title', url);
} else {
navigate(url);
history.pushState({url: url}, 'Blog - title', url);
}
};
}