First and second parameter in replaceState

Asked

Viewed 54 times

2

Good afternoon!

What is the first parameter in window.hostory.replaceState()

For example, in the examples I saw, they always pass an empty object...

Could explain exactly how this method works?

Already the second asks me the title, but when I change this title nothing happens, the title of the page remains the same, for what purpose then?

window.history.replaceState("object or string", "Title", "/new-url");

Thank you!

1 answer

1


Basically the first parameter of the window.hostory.replaceState() method, serves to pass some information through the history state, accessing history.state you will have the object passed in the parameter. The replaceState method works similarly to pushState, but instead of adding the current history input it modifies it. The basic use of this method, following the example of this site -> https://developer.mozilla.org/en-US/docs/Web/API/History_API

1 -Suppose you are on the page https://developer.mozilla.org/en-US/docs/Web/API/History_API

2 - You will add or modify the current history entry

const stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");

3 - When browsing to another page, for example - https://www.google.com/ and click on the arrow to return to the site Developer.mozilla.org, you will be redirected to the address entered in the third parameter of the pushState (in this case "bar.html") and accessing history.state you will have your stateObj object.

replaceState will change the information of the current record, if we were to use replaceState and put other information it would overwrite the data of our history.pushState.

That’s what I understood by reading the https://developer.mozilla.org/en-US/docs/Web/API/History_API, hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.