Fix anchors popstate bug

This commit is contained in:
Tissevert 2019-02-25 22:46:08 +01:00
parent 4368f30531
commit 2c29b331b8

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