重写history.pushState会导致opera 11中出现错误

发布于 2024-10-14 00:12:05 字数 736 浏览 1 评论 0原文

我通过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 技术交流群。

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

发布评论

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

评论(3

欢烬 2024-10-21 00:12:05

在 Opera 11.00,修订版 1156 中,支持的历史 API 如下:

>>> history.
back, current, forward, go, length, navigationMode

完整的 Opera 11.00 尚未涵盖 HTML5 历史记录 API。一般来说,如果您想发现、探索支持的内容,您可以轻松使用 dragonfly< 的控制台模式< /a>,Web 开发人员工具。

In Opera 11.00, Revision 1156, the history API supported are these

>>> history.
back, current, forward, go, length, navigationMode

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.

£烟消云散 2024-10-21 00:12:05

根据我什么时候可以使用…,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.

〃安静 2024-10-21 00:12:05

我想出了解决方案,只需在执行上述代码之前检查 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.

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