Sometimes, we want to fix anchor jumping by using JavaScript.
In this article, we’ll look at how to fix anchor jumping by using JavaScript.
How to fix anchor jumping by using JavaScript?
To fix anchor jumping by using JavaScript, we call the history.replaceState
method.
For instance, we write
const url = location.href;
location.href = "#" + hash;
history.replaceState(null, null, url);
to set the location.href
property to the url
.
Then we set it to a hash
.
Next, we call history.replaceState
to replacer the state with the url
to navigate to the URL with the hash without jumping.
Conclusion
To fix anchor jumping by using JavaScript, we call the history.replaceState
method.