简体   繁体   English

如何使用JavaScript侦听所有页面加载和刷新

[英]How to listen for all page loads and refreshes using Javascript

I am attempting to check for a cookie's existence whenever any kind of HTTP-related event occurs -- like the user refreshes a page or goes from one page to the next. 每当发生与HTTP相关的任何类型的事件时,我都会尝试检查Cookie的存在-例如用户刷新页面或从一个页面转到另一个页面。 If the cookie is found, a timer is activated starting from the cookie's value. 如果找到cookie,则从cookie的值开始激活计时器。

I tried on("page:load") , but it's not behaving like I thought it would. 我尝试了on("page:load") ,但是它的行为不像我想的那样。 Is page:load indeed the proper listener, or is there a more general way of encompassing all events after which global variables would be reset? page:load确实是正确的侦听器,还是有一种更通用的方式包含所有事件,之后将重置全局变量? Relevant code: 相关代码:

$(document).on("page:load",function(){
    if(isCookie('start_time') || $start_time) {
        timerRunningDisplay();
        activateTimer();            
    }
});

I would just use $(document).ready(function(){}); 我只用$(document).ready(function(){}); instead. 代替。 That will definitely fire when a page is loaded or refreshed. 当页面被加载或刷新时,那肯定会触发。 Then it's just a matter of making sure you're setting a referring to the cookie correctly, and you're golden. 然后,只需确保您正确设置了对Cookie的引用,就可以了。

So: 所以:

$(document).ready(function(){
    if(isCookie('start_time') || $start_time) {
        timerRunningDisplay();
        activateTimer();            
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM