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

How to prevent browser from saving webpage in cache, or force to request new page each time?

本文关键字:新页面 请求 浏览器 何防止 网页 保存 缓存 存在      更新时间:2023-09-26

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

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

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

您可以使用这些元标签:

<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>部分

有关元标签的更多信息,请点击此处

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

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;

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

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

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

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

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