Skip to content

Commit

Permalink
Accounting for nested elements when clicking on a link
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSpaar committed Feb 22, 2024
1 parent 968a43c commit ae3ddd4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 12 additions & 4 deletions doc/static/markdown_spa.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ window.addEventListener('popstate', async _ => {
});

window.addEventListener('click', e => {
if (!e.target.hasAttribute("href")) {
let targetAnchor = e.target.closest('a');
if (!targetAnchor || !targetAnchor.hasAttribute("href")) {
return;
}

if (e.target.href.startsWith(window.location.origin)) {
href = (targetAnchor) ? targetAnchor.href : e.target.href;
e.preventDefault();

if (href == window.location.href) {
return;
}

if (href.startsWith(window.location.origin)) {
e.preventDefault();
updatePage(e.target.href);
window.history.pushState({}, '', e.target.href);
updatePage(href);
window.history.pushState({}, '', href);
}
})
16 changes: 12 additions & 4 deletions src/markdown_spa/cli/blank/assets/markdown_spa.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ window.addEventListener('popstate', async _ => {
});

window.addEventListener('click', e => {
if (!e.target.hasAttribute("href")) {
let targetAnchor = e.target.closest('a');
if (!targetAnchor || !targetAnchor.hasAttribute("href")) {
return;
}

if (e.target.href.startsWith(window.location.origin)) {
href = (targetAnchor) ? targetAnchor.href : e.target.href;
e.preventDefault();

if (href == window.location.href) {
return;
}

if (href.startsWith(window.location.origin)) {
e.preventDefault();
updatePage(e.target.href);
window.history.pushState({}, '', e.target.href);
updatePage(href);
window.history.pushState({}, '', href);
}
})

0 comments on commit ae3ddd4

Please sign in to comment.