当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js清除浏览器缓存的几种方法

js清除浏览器缓存的几种方法

2019年03月20日  | 移动技术网IT编程  | 我要评论

关于浏览器缓存

浏览器缓存,有时候我们需要他,因为他可以提高网站性能和浏览器速度,提高网站性能。但是有时候我们又不得不清除缓存,因为缓存可能误事,出现一些错误的数据。像股票类网站实时更新等,这样的网站是不要缓存的,像有的网站很少更新,有缓存还是比较好的。今天主要介绍清除缓存的几种方法。

清理网站缓存的几种方法

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

其实form表单的缓存对于我们书写还是有帮助的,一般情况不建议清理,但是有时候为了安全问题等,需要清理一下!

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中)

方法五:

window.location.replace("webform1.aspx");  

参数就是你要覆盖的页面,replace的原理就是用当前页面替换掉replace参数指定的页面。 

这样可以防止用户点击back键。使用的是javascript脚本,举例如下:

a.html

以下是引用片段:

<html> 
   <head> 
     <title>a</title>   
     <script language="javascript"> 
       function jump(){ 
         window.location.replace("b.html"); 
       } 
     </script> 
   </head> 
   <body> 
    <a href="javascript:jump()" rel="external nofollow" rel="external nofollow" >b</a> 
  </body> 
</html> 

b.html

以下是引用片段:

<html> 
   <head> 
     <title>b</title>   
     <script language="javascript"> 
       function jump(){ 
         window.location.replace("a.html"); 
       } 
     </script> 
   </head> 
   <body> 
    <a href="javascript:jump()" rel="external nofollow" rel="external nofollow" >a</a> 
  </body> 
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网