javascript怎么清除缓存

javascript怎么清除缓存,第1张

javascript清除缓存方法

方式一:用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({

dataType:'json',

data:{},

cache:false,

ifModified :true ,

success:function(response){

// *** 作

}

async:false

})

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

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

eg:

<script>

document.write("<s"+"cript type='text/javascript' src='/js/test.js?"+Math.random()+"'></scr"+"ipt>")

</script>

其他的类似,只需在地址后加上+Math.random()

注意:因为Math.random() 只能在Javascript 下起作用,故只能通过Javascript的调用才可以

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

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

用PHP后端清理

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

方法五:

5、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()">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()">a</a>

</body>

</html>

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

清理网站缓存的几种方法

meta方法

<META HTTP-EQUIV="pragma" CONTENT="no-cache"> 

<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"> 

<META HTTP-EQUIV="expires" CONTENT="0">123

清理form表单的临时缓存 

方式一:用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

  })12345678910111213

方法二,直接用cache:false,

 $.ajax({

     url:'www.haorooms.com',

     dataType:'json',

     data:{},

     cache:false, 

     ifModified :true ,

     success:function(response){

         // *** 作

     }

     async:false

  })123456789101112

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

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

eg:

<script> 

document.write("<s"+"cript type='text/javascript' src='/js/test.js?"+Math.random()+"'></scr"+"ipt>") 

</script>

其他的类似,只需在地址后加上+Math.random() 

注意:因为Math.random() 只能在Javascript 下起作用,故只能通过Javascript的调用才可以 12345678

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

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

用php后端清理

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

方法五:

5、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()">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()">a</a> 

    </body> 

</html>

浏览器没有为js提供请缓存的接口

据检测,360浏览器的云中心有个清缓存的js接口,但只在指定的域名下可以调用清缓存的接口

这个要靠浏览器实现,目前没有浏览器对外开放

js防缓存可以将请求后加上时间戳,这样就不会读缓存的内容

页面级别防缓存一般用http头或meta


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9976685.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存