如何检测何时使用history.pushState和history.replaceState?
当历史状态修改时,我可以订阅一些事件吗?如何?
Is there some event I can subscribe to when the history state is modified? How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我曾经使用它来在调用
pushState
和replaceState
时收到通知:但这通常是矫枉过正。它可能不适用于所有浏览器。 (我只关心我的浏览器版本。)
注意。它在 Google Chrome 扩展内容脚本中也不起作用,因为它不允许更改网站的 JS 环境。您可以通过插入带有上述代码的
来解决这个问题,但这甚至更加过分了。
I used to use this to also be notified of when
pushState
andreplaceState
are called:It's usually overkill though. And it might not work in all browsers. (I only care about my version of my browser.)
NB. It also doesn't work in Google Chrome extension content scripts, because it's not allowed to alter the site's JS environment. You can work around that by inserting a
<script>
with said code, but that's even more overkill.