Html 强制浏览器重新加载 index.htm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26818091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Force browser to reload index.htm
提问by Linus
how can I force a browser to always load the newest version of index.htm when the page is loaded by entering the URL www.mydomain.com/index.htm or just www.mydomain.com in the browser's address field and pressing enter.
如何强制浏览器在页面加载时始终加载最新版本的 index.htm,方法是在浏览器的地址字段中输入 URL www.mydomain.com/index.htm 或仅 www.mydomain.com 并按 Enter。
I'm trying this in Chrome and the newest version of index.htm is apparently only loaded, when I refresh manually (F5), or when the URL is already in the browser's address field and I press enter.
我正在 Chrome 中尝试这个,最新版本的 index.htm 显然只加载,当我手动刷新 (F5) 时,或者当 URL 已经在浏览器的地址字段中时,我按下回车键。
I guess I am doing something extremely stupid, because when I searched for the issue, all I could find were solutions about how to make a browser reload your .js and .css files by appending ?v=xxxx to the file names. But how should this work, if not even the newest version of index.htm page, in which I am doing these modifiactions, is loaded??
我想我在做一些非常愚蠢的事情,因为当我搜索这个问题时,我能找到的只是关于如何通过将 ?v=xxxx 附加到文件名来让浏览器重新加载 .js 和 .css 文件的解决方案。但是,如果我正在做这些修改的最新版本的 index.htm 页面没有被加载,这应该如何工作?
I also tried putting
我也试过放
<meta http-equiv="cache-control" content="no-cache">
in the <head>
of index.htm. But this does not seem to have any effect.
在<head>
index.htm 中。但这似乎没有任何影响。
Any help would be greatly appreciated!
任何帮助将不胜感激!
Thanks, Linus
谢谢,莱纳斯
回答by Linus
OK, apparently no-cache was not enough. The following does the trick:
好吧,显然没有缓存还不够。以下是诀窍:
<meta http-equiv="cache-control" content="no-cache, must-revalidate, post-check=0, pre-check=0" />
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
回答by Linus
You can use the code below to refresh or reload the currently loaded index page from a URL address entered directly into the browser's address bar, after a specific number of seconds, thereby forcing the browser to always reload the current document. In this case, the number of seconds has been set to 5:
您可以使用以下代码在特定秒数后从直接输入浏览器地址栏的 URL 地址刷新或重新加载当前加载的索引页面,从而强制浏览器始终重新加载当前文档。在这种情况下,秒数已设置为 5:
<meta http-equiv="refresh" content="5" />
Please note that setting the number of seconds to 0 will cause the page to be automatically reloaded instantly, every time it is successfully downloaded.
请注意,将秒数设置为 0 将导致页面在每次成功下载时立即自动重新加载。
回答by Linus
To do this, you will need to perform some server-side coding along the way. You can use technologies like PHP or ASP.NET for this. I prefer coding with PHP, so here is a PHP-based example. First of all, make sure your INDEX is called "index.php", rather than "index.html" or "index.htm". Now take note of the following codes and make integrate this to your index.php file, while inserting your own page contents as well:
为此,您需要在此过程中执行一些服务器端编码。为此,您可以使用 PHP 或 ASP.NET 等技术。我更喜欢用 PHP 编码,所以这里有一个基于 PHP 的例子。首先,确保您的 INDEX 名为“index.php”,而不是“index.html”或“index.htm”。现在记下以下代码并将其集成到您的 index.php 文件中,同时插入您自己的页面内容:
<html>
<?php
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
?>
<head>
<script>
var sse = new EventSource("index.php");
sse.onmessage = function(event) {
document.write(event.data);
}
</script>
</head>
<body>
<!-- Insert HTML Codes -->
<?php
flush();
?>
</body>
</html>
回答by 807
Googled and came out on this website. maby this helps: http://code.tutsplus.com/tutorials/4-ways-to-auto-refresh-your-browser-when-designing-new-sites--net-13299Hope that helps, 807
谷歌搜索并出现在这个网站上。maby 这有帮助:http: //code.tutsplus.com/tutorials/4-ways-to-auto-refresh-your-browser-when-designing-new-sites--net-13299希望有所帮助,807