问题描述
基本上,我希望我的页面(比如page.html)检查page.html的新版本是否存在,如果是,请刷新页面,然后调出新版本。我意识到这会在一段时间后自动发生 - 但是,我希望在30秒到2分钟内发生这种情况。
我不确定这是怎么实现的 - 也许有些Javascript?
if(/ * *更新版本* /){
location.reload();
}
我不确定的部分是如何检测是否存在更新版本。
我有一个解决方法的想法 -
有一个函数,用于检查是否发送了某种信号(我会在发送后立即修改页面),一旦收到信号,刷新页面。
到目前为止,我唯一发现的是元刷新。
我之前没有想过使用它 - 但是,看起来我可能不得不这样做。
< META HTTP-EQUIV =refreshCONTENT =60>
我使用元刷新的唯一问题是它每分钟刷新我是否有新版本的该页面与否可能会非常烦人 - 尤其是因为我在页面上有图片。
是否有可能专门针对元素进行元刷新? (被引用的元素将是一个我经常修改的Javascript段,并且需要更新)
更新1
部分脚本:按下按钮时重新加载main.js。通过一些调整,我可以每分钟左右重新加载脚本。
Sandbox
< button id =refreshButtononclick = loadScript( '/ main.js') >
刷新脚本
< / button>
< script>
函数loadScript(位置){
var js = document.getElementById(sandboxScript);
if(js!== null){
document.body.removeChild(js);
console.info(---------- Script refreshed ----------);
}
//创建新的脚本元素并将脚本加载到它中
js = document.createElement(script);
js.src = location;
js.id =sandboxScript;
document.body.appendChild(js);
}
< / script>
最后,我发现了一个简单的解决方案,重新加载页面。
我链接了一些外部脚本(实际上,这是我需要重新加载的唯一东西)
$ b $然后我写了一个小函数(我将它添加到问题中),并且每10分钟重新获取外部脚本。像魅力一样工作,更好的是,它不在屏幕上显示,所以它不会中断用户!
Basically, I want my page (say page.html) to check if a newer version of page.html exists, and if so, refresh the page, and in doing so, bring up the newer version.
I realize this happens automatically after some time- however, I want this to happen within the time of 30 seconds to 2 minutes.
I'm not sure how this would be accomplished- perhaps some Javascript?
if(/*newer version exists*/) {
location.reload();
}
The part I'm unsure about is how to detect if a newer version exists.
I had an idea for a workaround-have a function that checks if some sort of "signal" has been sent (I'd send it right after modifying a page), and once the "signal" is received, refresh the page.
So far, the only thing I've found is meta refresh.I didn't think of using it before- however, it looks like I might have to.
<META HTTP-EQUIV="refresh" CONTENT="60">
The only issue I have with meta refresh is that it refreshes every minute whether I have a new version of the page or not, which can get very annoying- especially because I have images on the page.
Is it possible to specifically target an element for a meta refresh? (The element being referred to would be a Javascript segment which I frequently modify, and which needs to be up to date)
Update 1Partial Script: Reloads main.js when the button is pressed. With a few tweaks, I can reload the script every minute or so.
Sandbox
<button id="refreshButton" onclick="loadScript('/main.js')">
Refresh script
</button>
<script>
function loadScript(location) {
var js = document.getElementById("sandboxScript");
if(js !== null) {
document.body.removeChild(js);
console.info("---------- Script refreshed ----------");
}
// Create new script element and load a script into it
js = document.createElement("script");
js.src = location;
js.id = "sandboxScript";
document.body.appendChild(js);
}
</script>
In the end, I found a simpler solution than simply reloading the page.
I linked to some external script (which, in effect, was the only thing I needed reloaded)
I then wrote a little function (I added it to the question), and re-fetched the external script every 10 minutes. Works like a charm, and better yet, isn't visible onscreen, so it doesn't disrupt the user one bit!
这篇关于检查更新版本的页面,如果存在,重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!