Access the history state of the previous page when a user clicks 'back'

David Gard picture David Gard · Nov 14, 2011 · Viewed 8.6k times · Source

Is there a way using JS of accessing the history state of the previous page when a user clicks the 'back' button?

Answer

Maciej Pyszyński picture Maciej Pyszyński · Aug 14, 2012

Unfortunately you're not allowed to read previous or next states. All you can do is reading current state, using history.state.

But if it all happens on the same page without reloads, why don't you save data on you own stack. You can use array for this - just save the same data to array and new state.