清除浏览器缓存?

目的:清除浏览器缓存

浏览器缓存:有时候我们需要他,因为他可以提高网站性能和浏览器速度,提高网站性能。但是有时候我们又不得不清除缓存,因为缓存可能误事,出现一些错误的数据。

使用方法:JS或者Jquery

方法一: meta方法用客户端代码使浏览器不再缓存Web页面:

1
2
3
4
5
< head >
< meta    http-equiv = "Expires"    CONTENT = "0" >
< meta    http-equiv = "Cache-Control"    CONTENT = "no-cache" >
< meta    http-equiv = "Pragma"    CONTENT = "no-cache" >
</ head >

方法二: 清理form表单的临时缓存

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

还可以利用jquery ajax清除浏览器缓存

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$.ajax({
     url: '
,
     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,

1
2
3
4
5
6
7
8
9
10
11
12
13
$.ajax({
     url: '
,
     dataType:' json',
     data:{},
     cache: false
     ifModified : true  ,
 
     success: function (response){
         //操作
     }
     async: false
  });

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

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

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

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

方法五:用php后端清理

1
在服务端加 header( "Cache-Control: no-cache, must-revalidate" );

以上是网上常用的清除方法,另外在java中可以通过:JAVA后端清除Session缓存方法来实现

1
2
3
session.removeAttribute( "user" );  
session.removeAttribute( "constant_cache" );  
session.invalidate();

猜你喜欢

转载自www.cnblogs.com/duanzhange/p/9289745.html