What is replaceState method in JavaScript

history.replaceState() operates exactly like history.pushState() except that replaceState() modifies the current history entry instead of creating a new one. This method is useful when you want to update the URL or the state object for the current history.

It has 3 parameters: state object, page title, and a url.

state object:

It is a JavaScript object associated with the updated history entry done by replaceState() method. The state object can be anything but max of 640k characters in size.

title:

This parameter is currently ignored. You can pass a blank string at this place.

URL:

Pass the updated URL in this parameter.

Learn about pushState() method here.

Learn more about replaceState() method here.

Example of replaceState() method:

var stateObj = { foo: "bar" };
history.replaceState(stateObj, "Title of the page", "new-page-url.html");

Leave a Reply

Your email address will not be published. Required fields are marked *