如何防止浏览器在缓存中保存网页,或强制每次请求新页面?

时间:2012-02-05 13:35:27

标签: javascript jquery html

这可以通过添加HTML代码来完成,每次页面加载时都从服务器而不是缓存中请求页面吗?

4 个答案:

答案 0 :(得分:4)

唯一可行的选择是永远不要使用相同的URL两次,这是通过添加带有随机值的伪请求参数来完成的。

大多数客户端AJAX框架都是这样做的。

答案 1 :(得分:1)

您可以使用以下元标记:

<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">

在页面的<head>部分添加此内容

More info on meta tags here

答案 2 :(得分:1)

为避免缓存,您可以将时间戳附加为参数

var timestamp = new Date().getTime() 
  //append it to an url when you make an ajax call or to a link

 var myLink = document.getElementByID('yourlink');
 myLink.href = myLink.href + '&nocache='+timestamp;

答案 3 :(得分:0)

在head部分中,您可以添加 no-cache 指令。当设置Cache-Control:no-cache时,浏览器会将http请求发送到服务器并在释放缓存副本之前进行验证。

<meta http-equiv="Pragma" content="no-cache">

您使用 Expires 指令并将时间设置为请求时间之前的值,这将使浏览器为每个后续请求请求服务器。 max-age 指令也类似于Expires,可以用于此目的。

<meta http-equiv="Expires" content=-10>

看看这里:http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Avoiding_caching

相关问题