1:meta標簽
<meta http-equiv="pragma" content="no-cache"> <meta http-equiv="Cache-Control" content="no-cache, must-revalidate"> <meta http-equiv="expires" content="0">
2:清理form表單的臨時緩存
<body onLoad="javascript:document.yourFormName.reset()">
3: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();
關於緩存問題:js清除緩存
1、一般手動清除,瀏覽器緩存
2、js代碼清除緩存(原理:增加了一個參數,且該參數是一個隨機數,每次都不一樣,所以每次的請求參數都不一樣,服務器會將其作為一個新的請求,重新返回結果,而不會使用緩存)
js文件: 路徑后面加一個隨機數如下:
<script src="lib/gMarker.js?random=120211"></script>
css文件 <link rel='stylesheet' href='css/index.css?t=120224'> 加一個標識 強制瀏覽器重新加載此文件
圖片文件: background:url(**.png?20150421) 加一個標識號,使圖片不被緩存