清理网站缓存的几种方法

meta方法

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
<meta http-equiv="expires" content="0">

清理form表单的临时缓存

<body onLoad="javascript:document.yourFormName.reset()">

jquery ajax清除浏览器缓存

方式一:用ajax请求服务器最新文件,并加上请求头If-Modified-Since和Cache-Control,如下:

$.ajax({
     url:'www.haorooms.com',
     dataType:'json',
     data:{},
     beforeSend :function(xmlHttp){
        xmlHttp.setRequestHeader("If-Modified-Since","0");
        xmlHttp.setRequestHeader("Cache-Control","no-cache");
     },
     success:function(response){
         //操作
     }
     async:false
  });

方法二,直接用cache:false,

$.ajax({
    url:'www.haorooms.com',
    dataType:'json',
    data:{},
    cache:false,
    ifModified :true ,

    success:function(response){
        //操作
    }
    async:false
 });

方法三:用随机数,随机数也是避免缓存的一种很不错的方法!

URL 参数后加上 "?ran=" + Math.random(); //当然这里参数 ran可以任意取了

方法四:用随机时间,和随机数一样。

在 URL 参数后加上 "?timestamp=" + new Date().getTime();

用php后端清理

在服务端加 header("Cache-Control: no-cache, must-revalidate");等等(如php中)

关于缓存问题:js清除缓存

1、一般手动清除,浏览器缓存

2、js代码清除缓存(原理:增加了一个参数,且该参数是一个随机数,每次都不一样,所以每次的请求参数都不一样,服务器会将其作为一个新的请求,重新返回结果,而不会使用缓存)

js文件:  路径后面加一个随机数如下:

<script src="lib/gMarker.js?random=120211"></script>

 css文件 <link rel='stylesheet' href='css/index.css?t=120224'>   加一个标识 强制浏览器重新加载此文件

图片文件: background:url(**.png?20150421) 加一个标识号,使图片不被缓存

原文:https://www.cnblogs.com/wangyongx/p/10278520.html

02-13 17:14