問題描述
假設我在瀏覽器中打開了一個頁面,然后我轉到地址欄并輸入另一個頁面.然后我點擊后退按鈕轉到我的原始頁面.我想寫一些 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/
這篇關于Javascript:檢測頁面何時被導航“返回";到的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!