防止缓存 HTML 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16716695/
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
Prevent caching of HTML page
提问by Nate Pet
I have a HTML page. The problem is that I do not want to have the users to refresh the page each time I put on new content.
我有一个 HTML 页面。问题是我不想让用户每次添加新内容时都刷新页面。
I have the following code in order to make sure that the page is not cached:
我有以下代码以确保页面不被缓存:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
The problem is, I still need to do a refresh on the page in order to get the most current content to show up. Am I doing something wrong? Should I be using some other tags?
问题是,我仍然需要刷新页面才能显示最新的内容。难道我做错了什么?我应该使用其他标签吗?
采纳答案by Quentin
The values you have there are OK, but meta http-equiv
is highly unreliable. You should be using real HTTP headers (the specifics of how you do this will depend on your server, e.g. for Apache).
您在那里拥有的值是可以的,但meta http-equiv
非常不可靠。您应该使用真正的 HTTP 标头(具体如何执行取决于您的服务器,例如对于 Apache)。
回答by Garrin
The Codesnippet you showed makes the browser load the website everytime it accessesit, which is useful if you perform frequent updates, but still have a static page.
您展示的 Codesnippet 使浏览器在每次访问网站时加载网站,如果您执行频繁更新但仍然有静态页面,这将非常有用。
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
In case you want it to perform live updates, like it does for example in a (g)mail account, you need to make it refresh (parts of the page) itself. Use Javascript in this case, like it is shown in this questionor an ajax call.
如果您希望它执行实时更新,就像它在 (g) 邮件帐户中所做的那样,您需要使其刷新(页面的一部分)本身。在这种情况下使用 Javascript,就像在这个问题或 ajax 调用中所示。
$('#something').click(function() {
location.reload();
});