Can you get the previous history state object in js?

When I click the back or forward button and the popstate event fires, can I get the previous state object? Like in the state object provided by e.state and in what I just returned / removed?

Alternatively, can I determine if the back or forward button was pressed?

I need this since I have multiple subsystems that all need to use the js history at the same time, I could save the state of all of them when pushing of course, but then when I pop I need to restore the state of all of them also, which is undesirable, since it dumps objects that I don't need. for example

state 1 = {a:1, b:1, c:1}
push a = 2
state 2 = {a:2, b:1, c:1}
hit back button, popstate fires state 1 to me
restart a with 1, b with 1, c with 1 while I only need to restart a

      

Alternative fantasy solution

....
hit back button, popstate fires state 1 to me
I also get state 2 (the one I just moved away from) through some black magic
do a differential comparison between the 2 states, find out that I only need to modify a
restart a with 1

      

EDIT: oh also, to clarify, there is no easy way to check what state of a, b and c are currently on the page (since it is a little more complex than abc and 123)

+2


source to share


1 answer


onpopstate

runs after a state change, so you can't get the original state, but you can do it the other way around, for example, have a copy of the states and the current state indicator and update your copy in onpopstate

manually.



+3


source







All Articles