Reputation: 2150
I want something like document.referrer
. But since I'm building a single page app, with backbone
and pushstate
, the value is ""
.
How can I get something like history.prevState().url
.
EDIT: I do not need the event. I want to access info of the previous state anytime/anywhere I want.
Upvotes: 2
Views: 2325
Reputation: 43718
Perhaps you could simply store the last state in the new state object.
history.pushState({
lastState: 'some state'
}, '', 'newpage.html');
console.log(history.state.lastState); //some state
If you are building a single-page application, even if there are no standardization on how the state transition is initiated you could probably manage to intercept all of them by listening to the popstate
and the onhashchange
events.
You could then just push every states encountered in an array and store a serialized version of it in the localStorage
. That would allow you retrieving any previous states. However, this collection will grow so you would have to make sure that you control it's size. You could also simply keep the last state instead of all of them.
However, it would definitely be better to establish a standard across modules. Not only it would make accessing the previous state easier, but it would also be easier to maintain and more flexible in terms of future changes.
Upvotes: 1