你能在js中获得以前的历史状态对象吗? [英] Can you get the previous history state object in js?

查看:104
本文介绍了你能在js中获得以前的历史状态对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下向后或向前按钮并且popstate事件触发时,我可以获得前一状态的状态对象吗?因为不是e.state提供的状态对象,而是我刚刚退回/转发的状态对象?

When I hit the backwards or forwards button and the popstate event fires, can I get the state object of the previous state? As in not the state object provided by e.state but the one I just back/forwarded away from?

或者,我可以检测它是按下的后退还是前进按钮?

Alternatively, can I detect whether it was the back or forwards button that was pressed?

我需要这个,因为我有多个子系统都需要同时使用js历史记录,我当然可以在推送时保存所有子系统的状态但是然后,当我弹出时,我必须将状态恢复到所有这些状态,这是不希望的,因为它重置了我不需要的对象。例如

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 have to restore the state to all of them too which is undesired as it resets objects that I don't need to. 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

另类幻想解决方案

....
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

编辑:哦,为了澄清,没有简单的方法可以检查页面上当前的状态a,b和c(因为它比abc和123更复杂一些) )

oh also, to clarify, there is no easy way to check what state a, b and c currently are on the page (since its a bit more complicated than abc and 123)

推荐答案

onpopstate 在状态更改后触发,因此无法获取起源状态,但你可以反过来做,比如拥有状态和ind的副本当前状态的icator,并手动更新 onpopstate 中的副本。

onpopstate is fired after the state change so you cannot get the originating state, but you can do it the other way around, like having a copy of the states and indicator of current state, and update your copy in the onpopstate manually.

这篇关于你能在js中获得以前的历史状态对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆