Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve if the popstate event comes from back or forward actions with the HTML5 pushstate?

I'm developing a webpage where depending on the next or back actions I do the correspondent animation, the problem comes when using the pushstate. When I receive the event how do I know if the user clicked back or forward history buttons using the Pushstate API?, or do I have to implement something myself?

like image 800
Davsket Avatar asked Jan 23 '12 23:01

Davsket


People also ask

Does history back trigger Popstate?

The popstate event will be triggered by doing a browser action such as a click on the back or forward button (or calling history. back() or history.

What is a Popstate event?

PopStateEvent is an interface for the popstate event. A popstate event is dispatched to the window every time the active history entry changes between two history entries for the same document. If the history entry being activated was created by a call to history. pushState() or was affected by a call to history.


1 Answers

You must implement it yourself which is quite easy.

  • When invoking pushState give the data object a unique incrementing id (uid).
  • When onpopstate handler is invoked; check the state uid against a persistent variable containing the last state uid.
  • Update the persistent variable with the current state uid.
  • Do different actions depending on if state uid was greater or less than last state uid.
like image 157
bennedich Avatar answered Sep 30 '22 16:09

bennedich