Javascript:检测页面何时被导航“返回";到

2022-01-10 00:00:00 jquery navigation javascript html

假设我在浏览器中打开了一个页面,然后我转到地址栏并输入另一个页面.然后我点击后退按钮转到我的原始页面.我想写一些 Javascript 代码来检测这种情况并做出响应.

Suppose I have a page open in a browser and I go to my address bar and enter another page. Then I hit the back button to go to my original page. I'd like to write some Javascript code that can detect this scenario and respond to it.

尽我所知,在这种情况下,ready 事件、onload 以及页面本身上的任何内联 Javascript 都不会重新执行.还有什么我可以做的吗?

As best I can tell neither the ready event, the onload, nor any inline Javascript on the page itself is re-executed in this scenario. Is there anything else I can do?

跨浏览器支持在这里很重要.首选但不是必需的基于 jQuery 的解决方案.

Cross-browser support is important here. jQuery based solutions preferred but not required.

为清楚起见进行我假设的导航是页面 A -> 页面 B -> 返回页面 A,我假设页面 B 可能在我的控制之下,也可能不在我的控制之下.

Edit for clarity: the navigation I'm assuming is Page A -> Page B -> back to page A where I'm assuming that Page B may or may not be under my control.

推荐答案

在 Firefox 中,您可以检查 DomContentLoaded 事件.对于跨浏览器的解决方案,需要做更多的工作:

In Firefox, you can check for the DomContentLoaded event. For a cross-browser solution, a little more work is required:

http://dean.edwards.name/weblog/2005/09/busted/

相关文章