如何每隔n秒从地址栏自动重新加载页面?

时间:2014-06-12 20:55:05

标签: javascript html

为此寻找跨平台解决方案。我可以访问未实现自动刷新的Intranet站点。我尝试了这段代码,但它只刷新了一次页面。我希望它每隔5秒刷新一次,并从服务器刷新,而不是缓存。

javascript:void(setInterval(function(){window.location.reload();},5000))

欢迎任何提示。

1 个答案:

答案 0 :(得分:0)

试试这个。它在Chrome中运行,没有在其他地方测试它,但我没有看到它不应该工作的原因:

javascript:document.documentElement.style.height%3D%22100%25%22%3Bdocument.body.style.height%3D%22100%25%22%3Bdocument.body.style.margin%3D0%3Bdocument.body.style.padding%3D0%3Bdocument.body.innerHTML%3D%22%3Ciframe%20width%3D100%25%20height%3D100%25%20frameborder%3D0%3E%3C%2Fiframe%3E%22%3Bvar%20frame%3Ddocument.getElementsByTagName(%27iframe%27)%5B0%5D%3BsetInterval(function()%7Bframe.src%3Ddocument.location%7D%2C%205000)%3Bframe.src%3Ddocument.location%3B

注意:当您将其复制粘贴到Chrome的地址栏时,它显然从一开始就剥离javascript:,我想出于安全原因)。确保将其添加回来。

逐行:

// Making sure the document doesn't have margin or padding
// and it takes up the whole screen
document.documentElement.style.height="100%";
document.body.style.height="100%";
document.body.style.margin=0;
document.body.style.padding=0;

// Replacing the current contents with an iframe that 
fills the whole document
document.body.innerHTML="<iframe width=100% height=100% frameborder=0></iframe>";

// Finding the iframe
var frame=document.getElementsByTagName('iframe')[0];

// Scheduling refresh in every 5s
setInterval(function(){frame.src=document.location}, 5000);

// Loading the original page in the iframe
// for the first time
frame.src=document.location;