Sometimes, we want to update window.location.hash without jumping the document with JavaScript.
In this article, we’ll look at how to update window.location.hash without jumping the document with JavaScript.
How to update window.location.hash without jumping the document with JavaScript?
To update window.location.hash without jumping the document with JavaScript, we can use the history.pushState
method or set the value of location.hash
.
For instance, we write
if (history.pushState) {
history.pushState(null, null, "#hash");
} else {
location.hash = "#hash";
}
to call history.pushState
with the hash string as the 3rd argument when it’s available.
Otherwise, we set location.hash
to the hash string instead.
Conclusion
To update window.location.hash without jumping the document with JavaScript, we can use the history.pushState
method or set the value of location.hash
.