關(guān)于瀏覽器緩存
瀏覽器緩存,有時(shí)候我們需要他,因?yàn)樗梢蕴岣呔W(wǎng)站性能和瀏覽器速度,提高網(wǎng)站性能。但是有時(shí)候我們又不得不清除緩存,因?yàn)榫彺婵赡苷`事,出現(xiàn)一些錯(cuò)誤的數(shù)據(jù)。像股票類網(wǎng)站實(shí)時(shí)更新等,這樣的網(wǎng)站是不要緩存的,像有的網(wǎng)站很少更新,有緩存還是比較好的。今天主要介紹清除緩存的幾種方法。
清理網(wǎng)站緩存的幾種方法
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表單的臨時(shí)緩存
<body onLoad="javascript:document.yourFormName.reset()">
其實(shí)form表單的緩存對(duì)于我們書寫還是有幫助的,一般情況不建議清理,但是有時(shí)候?yàn)榱税踩珕栴}等,需要清理一下!
jquery ajax清除瀏覽器緩存
方式一:用ajax請(qǐng)求服務(wù)器最新文件,并加上請(qǐng)求頭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 });方法三:用隨機(jī)數(shù),隨機(jī)數(shù)也是避免緩存的一種很不錯(cuò)的方法!
URL 參數(shù)后加上 "?ran=" + Math.random(); //當(dāng)然這里參數(shù) ran可以任意取了
方法四:用隨機(jī)時(shí)間,和隨機(jī)數(shù)一樣。
在 URL 參數(shù)后加上 "?timestamp=" + new Date().getTime();
用php后端清理
在服務(wù)端加 header("Cache-Control: no-cache, must-revalidate");等等(如php中)
方法五:
window.location.replace("WebForm1.aspx");  
參數(shù)就是你要覆蓋的頁面,replace的原理就是用當(dāng)前頁面替換掉replace參數(shù)指定的頁面。
這樣可以防止用戶點(diǎn)擊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>以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注