Javascript 如何自动重新加载网页

Javascript 如何自动重新加载网页,javascript,for-loop,web,break,Javascript,For Loop,Web,Break,我希望我的网站页面在第一次打开时重新加载一次。我在javascript文件中为此编写了此函数 var i; $(document).ready(function(){ for ( i=0;i<1;i++){ if(i===0){ location.reload(); break; } } }); vari; $(文档).ready(函数(){ 对于(i=0;i),必须设置cookie(或使

我希望我的网站页面在第一次打开时重新加载一次。我在javascript文件中为此编写了此函数

var i;
$(document).ready(function(){
    for ( i=0;i<1;i++){
        if(i===0){
            location.reload();
            break;
        }  
    }
});
vari;
$(文档).ready(函数(){

对于(i=0;i),必须设置cookie(或使用javascript的localStorage),或使用xhr检索远程服务器上保存的值

如果你想使用cookies,它很简单

document.cookie=“username=John Doe”

其中document.cookie是以下形式的查询字符串(
x=y;a=b;n=z


如果您希望在用户每次访问时重新加载页面,请确保在设置页面重新加载后,在完成任何必要的处理后取消设置cookie。

下面是发生的情况:

  • 页面第一次加载时,jQuery调用document.ready事件上的任何处理程序

  • 页面将重新加载

  • 再次进行document.ready调用

  • 重复

出于好奇,为什么要这样做?为什么要有一个for循环来运行一次迭代


另外,据我所知,要回答您的问题,确保页面不重新加载的唯一方法是使用持续约5秒的cookie。然后,在document.ready上检查cookie,如果存在,则不重新加载。


$( window ).load(function() {
    if (window.location.href.indexOf('reload')==-1) {
         window.location.replace(window.location.href+'?reload');
    }
});
(功能(){ 如果(window.localStorage){ 如果(!localStorage.getItem('firstLoad')){ localStorage['firstLoad']=true; window.location.reload(); }否则 localStorage.removietem('firstLoad'); } })();
您的代码每次执行$(document).ready(),因此循环无限大也就不足为奇了——每次加载都以就绪状态完成

如果您给出更详细的需求,我们可以解决它,而不使用窗口对象作为数据保持器。这是一种不好的方法,但您可以将其设置为测试

窗口对象存储的变量不依赖于重载,因为它比文档高

让我们试试:

if( window.firstLoad == undefined  ){
    // yours code without any loop
    // plus:
    window.firstLoad = false;
}
您可以使用localStorage API实现

也请检查此链接,它提供了有关窗口对象变量的更多信息:

代码是可以的。但是如果从另一个带有id(…/page.html#aa)链接的页面打开该页面,则该代码仅适用于firefox。在其他浏览器中,重新加载页面而不使用id。(对不起,我的英语).

我找到了使用此代码的解决方案。假定页面刷新时间不早于一小时。否则,请在oggindex变量中添加分钟

<script>
var pagina = window.location.href; 
var d = new Date();
var oggiindex = d.getMonth().toString()+'-'+d.getDate().toString()+'-'+d.getHours().toString();
if (localStorage.ieriindex != oggiindex)
    {
    localStorage.setItem("ieriindex", oggiindex);
    window.location.replace(pagina);
    }
 </script>  

var pagina=window.location.href;
var d=新日期();
var oggiindex=d.getMonth().toString()+'-'+d.getDate().toString()+'-'+d.getHours().toString();
if(localStorage.ieriindex!=oggiindex)
{
setItem(“ieriindex”,oggiindex);
窗。位置。更换(pagina);
}

此要求的原因是什么?您的问题是,一旦重新加载页面,
i
变量返回到0(您将进入循环)。您可以使用cookie/localstorage保存页面已加载一次的事实(以防止再次加载)。为什么要这样做?!它不是递归的,只是每次再次加载时,它都会触发一个新的
就绪
(函数(){if(window.localStorage){if(!localStorage.getItem('firstLoad')){localStorage['firstLoad']=true;window.location.reload();}else localStorage.removietem('firstLoad'));   } })()
不要使用循环..只需在页面完全加载时重新加载页面..在就绪事件函数或加载事件函数中cookie与localStorage不同。cookie不是本地存储?cookie保存在本地,但这与我接受localStorage不同于本地存储,而是本地存储的概念不同明确适用于Cookie。我将适当更新答案,以避免混淆。谢谢,我发表了评论,因为我认为会有一些混淆,尤其是对于新用户。很聪明,但最好设置
窗口。位置。搜索
,而不是在
href
的末尾添加字符串。您能添加解释吗对那些跟上线索的人来说,这是有道理的,但对后来犯下错误的人却没有什么意义。
<script>
var pagina = window.location.href; 
var d = new Date();
var oggiindex = d.getMonth().toString()+'-'+d.getDate().toString()+'-'+d.getHours().toString();
if (localStorage.ieriindex != oggiindex)
    {
    localStorage.setItem("ieriindex", oggiindex);
    window.location.replace(pagina);
    }
 </script>