1. 程式人生 > >JS強制刷新頁面、清除緩存刷新

JS強制刷新頁面、清除緩存刷新

.com oms control fun eval mod function 方式 nload

清理網站緩存的幾種方法

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表單的臨時緩存

<body onLoad="javascript:document.yourFormName.reset()">

jquery 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();

用php後端清理

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

JS強制刷新頁面、清除緩存刷新