Pushing on the history stack - history.replaceState()

 The history.replaceState() method is exactly like history.pushState(), the only difference is that it replaces the current page with another, that is, if you use history.pushState() and press the back button, you'll be directed to the page you came from.

However, when you use history.replaceState() and you press the back button, you are not directed to the page you came from because it is replaced with the new one on the stack. Here's an example of working with the replaceState method:

history.replaceState({myName: "Mehul"}, "This is title of page", "/i_am_awesome");

This replaces (instead of pushing) the current state with the new state.

Although using the History API directly in your ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.