使用replaceState()(或其他方法)更新状态对象而不是替换它

发布于 2024-12-20 01:02:24 字数 559 浏览 3 评论 0原文

我可以使用一些技术(也许是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-12-27 01:02:24

自 Chrome 8 起,History.js 允许通过 ReplaceState 进行状态操作。

我目前遇到了类似的问题,其中只需要修改状态的一部分。这是通过克隆当前状态数据、更改值然后调用replaceState 来完成的。

History.replaceState(newState, null, oldState.url);

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.

History.replaceState(newState, null, oldState.url);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文