Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.history.pushState() data, how and when they are retrieved?

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?

like image 721
Yuri Nko Avatar asked Jun 18 '17 16:06

Yuri Nko


People also ask

What does Window history pushState do?

pushState() method adds an entry to the browser's session history stack.

How does Window history work?

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.

Does history pushState reload page?

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.


1 Answers

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
});
like image 138
jlewkovich Avatar answered Oct 21 '22 17:10

jlewkovich