使用replaceState()(或其他方法)更新状态对象而不是替换它
我可以使用一些技术(也许是 replaceState()
的巧妙方法)来更新历史状态对象而不是替换它?
给定一个如下所示的现有状态对象:
{ prop1:someValue, prop2:someOtherValue }
我希望能够调用将修改 prop1
但保留 prop2
的东西。
我想我真的在寻找 modifyState()
而不是 replaceState()
。
如果我知道 prop2
的值是什么,那么这将是微不足道的,但我不知道。 Firefox 允许您直接读取 history.state
,因此我可以在 Firefox 上找到该值并使用 replaceState()
。然而,这不适用于 Chrome、Safari 等。因为(据我所知)他们不允许直接访问history.state
。
有什么方法可以让我如上所述修改状态对象吗?
Is there some technique I can use (perhaps something clever with replaceState()
) to update the history state object rather than replace it?
Given an existing state object that looks like this:
{ prop1:someValue, prop2:someOtherValue }
I want to be able to call something that will modify prop1
but leave prop2
alone.
I guess I'm really looking for a modifyState()
rather than replaceState()
.
This would be trivial if I knew what the value of prop2
was, but I don't. Firefox allows you to read history.state
directly, so I could find the value out on Firefox and go with replaceState()
. However that won't work for Chrome, Safari, et al. because (as far as I can tell) they don't allow direct access to history.state
.
Is there some way for me to modify the state object as described above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自 Chrome 8 起,History.js 允许通过 ReplaceState 进行状态操作。
我目前遇到了类似的问题,其中只需要修改状态的一部分。这是通过克隆当前状态数据、更改值然后调用replaceState 来完成的。
History.js allows for state manipulation via replaceState since Chrome 8.
I currently have a similar issue where only a part of the state needs to be modified. This is accomplished by cloning the current state data, changing the value and then calling replaceState.