1. 程式人生 > >清理網站快取的幾種方法

清理網站快取的幾種方法

清理網站快取的幾種方法

meta方法

?

1

2

3

4

//不快取

<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,如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

$.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,

?

1

2

3

4

5

6

7

8

9

10

11

$.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

以下是引用片段:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

<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

以下是引用片段:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

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