Why does my page reload every time I click a link? How can I stop it? or Is there a way to prevent un

18 Replies, 1790 Views

This might sound dumb, but are you sure the link isn’t just a `#`? Empty hashes can cause page reloads.

Try this:
```js
document.querySelectorAll('a').forEach(link => {
link.addEventListener('click', e => {
if (link.getAttribute('href') === '#') {
e.preventDefault();
}
});
});
```
If that doesn’t help, maybe share a snippet of your code?

Messages In This Thread



Users browsing this thread: 1 Guest(s)