window.history.pushState([data], 'Title', '/url');
When I set up data in here, how can I use them and what kind of data this is meant for?
pushState() method adds an entry to the browser's session history stack.
The Window. history read-only property returns a reference to the History object, which provides an interface for manipulating the browser session history (pages visited in the tab or frame that the current page is loaded in). See Manipulating the browser history for examples and details.
But this function is not intended to reload the browser. All the function does, is to add (push) a new "state" onto the browser history, so that in future, the user will be able to return to this state that the web-page is now in.
The data
object is meant to store structured data of your choosing associated with the history item, so that when the state is revisited, the application has some data available associated with it. Maybe you can save the location of the page that the user was previously viewing, or some form options they had entered but never submitted.
Browsers will call the popstate()
method when a back button is fired, which will pop the most recent state pushed to the stack. Developers should add an event listener to the window
object for custom handling of the popstate event (such as using the data associated with the state).
// Below function will get fired for every app-wide popstate
window.addEventListener('popstate', function(event) {
// Can access state data using event.state.data
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With