重写history.pushState会导致opera 11中出现错误
我通过greasemonkey脚本/opera扩展将以下代码注入到网页中以捕获history.pushState
命令,这样我就可以在它被触发时进行一些处理,并且仍然允许pushState< /code> 命令随后继续。
(function(history){
var pushState = history.pushState;
history.pushState = function(state) {
if (typeof history.onpushstate == "function") {
history.onpushstate({state: state});
}
alert('pushstate called')
return pushState.apply(history, arguments);
}
})(window.history);
该代码在 FF4 和 Chrome 中工作正常,但在 Opera 11 中,如果页面调用 history.replaceState
命令,我会收到以下错误:
未捕获的异常:TypeError:'window.history.replaceState'不是函数
有谁知道如何修复上述代码以与 Opera 以及 Chrome 和 Firefox 一起使用?
I'm injecting the following code into a webpage via a greasemonkey script/opera extension to trap the history.pushState
command, so I can do some processing whenever it's fired and still allow the pushState
command to continue afterwards.
(function(history){
var pushState = history.pushState;
history.pushState = function(state) {
if (typeof history.onpushstate == "function") {
history.onpushstate({state: state});
}
alert('pushstate called')
return pushState.apply(history, arguments);
}
})(window.history);
the code works fine in FF4 and Chrome, but in Opera 11, I get the following error, if the page calls a history.replaceState
command:
Uncaught exception: TypeError: 'window.history.replaceState' is not a function
Does anyone know how I can fix the above code to work with Opera as well as Chrome and Firefox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Opera 11.00,修订版 1156 中,支持的历史 API 如下:
完整的 Opera 11.00 尚未涵盖 HTML5 历史记录 API。一般来说,如果您想发现、探索支持的内容,您可以轻松使用 dragonfly< 的控制台模式< /a>,Web 开发人员工具。
In Opera 11.00, Revision 1156, the history API supported are these
The full HTML5 history API is not yet covered by Opera 11.00. In general if you would like to discover, explore what is supported, you can easily use the console mode of dragonfly, the Web developer tool.
根据我什么时候可以使用…,Opera 还不支持 History API,所以这就是你得到这个异常的原因。
According to When can I use … Opera doesn't support the History API yet, so that's why you get that exception.
我想出了解决方案,只需在执行上述代码之前检查
history.replacestate
,如果不存在,则不执行代码,简单。I figured out the solution, just check for
history.replacestate
before executing the above code, if it doesn't exist, don't execute the code, simple.