How to change the URL without reloading the page with JavaScript?

Sometimes, we want to change the URL without reloading the page with JavaScript.

In this article, we’ll look at how to change the URL without reloading the page with JavaScript.

How to change the URL without reloading the page with JavaScript?

To change the URL without reloading the page with JavaScript, we can use window.history.pushState.

For instance, we write:

window.history.pushState("state", "title", "/new-url");

to change the URL to /new-url.

The first argument is the state, which can be anything that is serializable.

Then 2nd argument is the title, which is ignored by most browsers.

Conclusion

To change the URL without reloading the page with JavaScript, we can use window.history.pushState.