diff --git a/static/markdown_spa.js b/static/markdown_spa.js index 541849a..931d672 100644 --- a/static/markdown_spa.js +++ b/static/markdown_spa.js @@ -1,6 +1,17 @@ if (typeof preFetch !== 'function') { function preFetch() {} } if (typeof postFetch !== 'function') { function postFetch() {} } +function overrideLinks() { + document.querySelectorAll('a').forEach(a => { + if (a.href.startsWith(window.location.origin) && !a.hasAttribute('onclick')) { + a.onclick = e => { + e.preventDefault(); + updatePage(a.href); + }; + } + }); +} + async function updatePage(path) { preFetch(); @@ -11,11 +22,15 @@ async function updatePage(path) { document.documentElement.innerHTML = text; } - window.history.pushState({}, '', path); + overrideLinks(); postFetch(); + + window.history.pushState({}, '', path); } postFetch(); +overrideLinks(); + let curPath = window.location.pathname; window.addEventListener('popstate', async _ => { @@ -26,12 +41,3 @@ window.addEventListener('popstate', async _ => { curPath = window.location.pathname; await updatePage(window.location.href); }); - -window.addEventListener('click', e => { - if (e.target.tagName == 'A' && e.target.href.startsWith(window.location.origin)) { - e.preventDefault(); - - if (e.target.href != window.location.href) - updatePage(e.target.href); - } -}); \ No newline at end of file