Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.