Update Browser URL Without Page Reload (JavaScript)

When building a single page web app it’s important to update the URLs without refreshing or reloading the page. This will also enable the user to easily copy and share or revisit the current page.

Using the History API

Browsers provide a History API that allows the page URL to be updated that works great for this use case. The API does allow for sharing other data but has limitations; with that in mind it would be better to find another way to share any data especially if it’s sizable.

Here’s an example of the history.pushState method. It accepts three arguments:

history.pushState({msg: 'hello'}, '', '/page2');

Check out a demo of the history.pushState() method combined with popstate.

Open demo in new tab

Reference