使用window.history.pushState函数时,未能在'历史记录'错误上执行'pushState' [英] Failed to execute 'pushState' on 'History' error when using window.history.pushState function

查看:669
本文介绍了使用window.history.pushState函数时,未能在'历史记录'错误上执行'pushState'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavascriptMVC应用程序中使用 window.history 来为每个控制器启用后退/前进/刷新功能。每次我加载一个新的控制器时,我都会使用 window.history.pushState 为历史添加一个新状态。然后在返回/刷新我使用保存的状态,并重新使用数据来再次构建控制器。



除了特定场景的一个问题外,整个想法都很好。我收到以下错误:


在'历史'上执行'pushState'失败:对象不能是
克隆。


在其他情况下添加相同的数据没有问题。什么可能导致此错误?
感谢您的帮助。

解决方案

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history



状态对象可以是任何可以序列化的对象,因为Firefox会将状态对象保存到用户的磁盘中,以便在用户重新启动浏览器后可以恢复它们,所以我们将大小限制为640k如果你将一个状态对象的序列化表示大于这个pushState(),这个方法会抛出一个异常,如果你需要更多的空间,建议你使用sessionStorage和/或localStorage。



看起来很简单的答案是,您传递的状态可能会序列化为大于640k。我刚碰到这个bug,几乎可以肯定是原因。


I'm using window.history in JavascriptMVC application to enable back/forward/refresh functionality for each controller. Every time I load a new controller I'm using window.history.pushState to add a new state to history. And then on back/refresh I'm using the saved state and reuse the data to build the controller again.

The whole idea works fine excepting one issue on specific scenario. I'm getting the following error:

Failed to execute 'pushState' on 'History': An object could not be cloned.

The same data is added without problem on other scenario. What can cause this error? Thanks for the assistance.

解决方案

https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

"The state object can be anything that can be serialized. Because Firefox saves state objects to the user's disk so they can be restored after the user restarts the browser, we impose a size limit of 640k characters on the serialized representation of a state object. If you pass a state object whose serialized representation is larger than this to pushState(), the method will throw an exception. If you need more space than this, you're encouraged to use sessionStorage and/or localStorage."

Looks like the simple answer is that possible the state you are passing in is serializing to larger than 640k. I just ran into this bug, and I am almost certain that is the cause.

这篇关于使用window.history.pushState函数时,未能在'历史记录'错误上执行'pushState'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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