window.location.reload完成?

我打电话

window.location.reload(false) 

在一个JavaScript方法来更新页面。 这个电话后,我有额外的JavaScript调用。 有没有办法知道何时调用额外的JavaScript调用window.location.reload(false)已经完成运行?

你只需要提供一个函数onload:重载与载入没有区别。

 window.onload = function() { // ... 

你的代码行window.location.reload(false)导致浏览器重新加载当前页面 – 没有脚本后,将执行此语句….

你可以设置一个cookie在第一次加载 – 然后在后续的负载检查存在的cookie和执行一个动作…伪代码:

 onload = check cookie if cookie is present run function else set cookie reload 

您可以检查cookie上的时间,并选择执行一段时间后(例如1小时)已经通过….

我使用hashtag来设置变量,告诉我页面是否被重新加载。 你可以做这样的事情:

 // Get the hash of the page var hashstring = window.location.hash.substring(1); var found = false; // Do a hash exist? if (hashstring.length > 0) { // Split the hash by '&'-sign (in case you have more variables in the hash, as I have) var a = hashstring.split("&"); // Loop through the values for (i = 0; i < a.length; i++) { // Split the string by '=' (key=value format) var b = a[i].split("="); // If the key is 'reloaded' (which tells us if the page is reloaded) if(b[0] == 'reloaded') { found = true; } } } if(!found) { location.hash = 'reloaded=true'; window.location.reload(); } // Do other stuff, this will only be executed if the page has been reloaded 

我已经把我的项目中的代码发现一个变量散列在一个单独的函数,但很简单,我只是在上面添加它。 这使得可以确定页面是否已经被重新加载,并且只有在已经存在的情况下才运行代码。