清除瀏覽器緩衝的幾種方法

來源:互聯網
上載者:User
關於瀏覽器緩衝

瀏覽器緩衝,有時候我們需要他,因為他可以提高網站效能和瀏覽器速度,提高網站效能。但是有時候我們又不得不清除緩衝,因為緩衝可能誤事,出現一些錯誤的資料。像股票類網站即時更新等,這樣的網站是不要緩衝的,像有的網站很少更新,有緩衝還是比較好的。今天主要介紹清除緩衝的幾種方法。 清理網站緩衝的幾種方法

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()">

其實form表單的緩衝對於我們書寫還是有協助的,一般情況不建議清理,但是有時候為了安全問題等,需要清理一下。

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中)
C# HttpClient禁止緩衝
           using (var client = new HttpClient())            {                //方法1:                 CacheControlHeaderValue cacheControl = new CacheControlHeaderValue();                cacheControl.NoCache = true;                cacheControl.NoStore = true;                client.DefaultRequestHeaders.CacheControl = cacheControl;                //方法2:                 //client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");                try                {                    client.GetStringAsync(url);                    return true;                }                catch                {                    return false;                }            }
C#頁面清除緩衝
        private void SetPageNoCache()         {            Response.Buffer = true;            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);            Response.Expires = 0;            Response.CacheControl = "no-cache";            Response.AddHeader("Pragma", "No-Cache");        }

任意一句即可:

    (1)     Response.Buffer = true;             Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-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">    (3) 重新調用原頁面的時候在給頁面傳一個參數:    href="****.aspx?random()"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.