禁用頁面緩存的幾種方法(靜態和動態)

1、在Asp頁面首部<head>加入   

  
Response.ExpiresAbsolute   =   Now()   -   1    
  Response.Expires   =   0    
  Response.CacheControl   =   "no-cache"    
  Response.AddHeader   "Pragma",   "No-Cache"  

  2、在HtML代碼中加入   

  
<META   HTTP-EQUIV="Pragma"   CONTENT="no-cache">    
  
<META   HTTP-EQUIV="Cache-Control"   CONTENT="no-cache">    
  
<META   HTTP-EQUIV="Expires"   CONTENT="0">    
  
</HEAD>  
 

    
  3、在重新調用原頁面的時候在給頁面傳一個參數   Href="****.asp?random()" 

 

  前兩個方法據說有時會失效,而第三種則是在跳轉時傳一個隨機的參數! 因爲aspx的緩存是與參數相關的,如果參數不同就不會使用緩存,而會重新生成頁面,每次都傳一個隨機的參數就可以避免使用緩存。這個僅適用於asp&asp.net

  4、在jsp頁面中可使用如下代碼實現無緩存:

 


response.setHeader("Pragma","no-cache"); //HTTP 1.0 
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server

 

  這些代碼加在<head> </head>中間具體如下

 

<% 
response.setHeader(
"Cache-Control","no-cache"); //HTTP 1.1 
response.setHeader(
"Pragma","no-cache"); //HTTP 1.0 
response.setDateHeader (
"Expires"0); //prevents caching at the proxy server 
%> 
</head>
 

 


  5、window.location.replace("WebForm1.aspx");  
  參數就是你要覆蓋的頁面,replace的原理就是用當前頁面替換掉replace參數指定的頁面。   
這樣可以防止用戶點擊back鍵。使用的是javascript腳本,舉例如下:

  a.html

 

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

 

   b.html

 

 


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

 

  前4種只是清空了cache,即存儲在Temporary Internet Files文件夾中的臨時文件,而第五種則是使用跳轉頁面文件替換當前頁面文件,並沒有清空cache,也就是說Temporary Internet Files產生了相關的臨時文件,兩者搭配使用真是清空緩存,必備良藥。正好我這裏有了記錄,所以常來看看哦。

 
發佈了47 篇原創文章 · 獲贊 4 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章