-
Is there a way to make the browser not register anchor clicks? I want the user to click back button (after clicking on an anchor) and not go back to when they clicked the anchor but to the previous page. |
Beta Was this translation helpful? Give feedback.
Answered by
allejo
Apr 3, 2021
Replies: 1 comment 3 replies
-
This would have to be something you do via JS. The concept would be to intercept any clicks on the anchors, and manage updating the URL yourself in a way that doesn't trigger the history functionality. Here's an example of how you can achieve that behavior, <style>
.heading {
margin-bottom: 90vh;
}
</style>
<ul>
<li><a href="#section-1">Section 1</a></li>
<li><a href="#section-2">Section 2</a></li>
<li><a href="#section-3">Section 3</a></li>
<li><a href="#section-4">Section 4</a></li>
</ul>
<h1 class="heading" id="section-1">Section 1</h1>
<h1 class="heading" id="section-2">Section 2</h1>
<h1 class="heading" id="section-3">Section 3</h1>
<h1 class="heading" id="section-4">Section 4</h1>
<script>
document.querySelectorAll('a').forEach(el => {
el.addEventListener('click', event => {
event.preventDefault();
const hash = el.getAttribute('href');
window.location.replace(('' + window.location).split('#')[0] + hash);
})
})
</script> |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
allejo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This would have to be something you do via JS. The concept would be to intercept any clicks on the anchors, and manage updating the URL yourself in a way that doesn't trigger the history functionality.
Here's an example of how you can achieve that behavior,