js清除瀏覽器緩存的幾種方法
阿新 • • 發佈:2018-09-03
html 是你 -s ons 清理 asp .ajax 更新 asc
關於瀏覽器緩存
瀏覽器緩存,有時候我們需要他,因為他可以提高網站性能和瀏覽器速度,提高網站性能。但是有時候我們又不得不清除緩存,因為緩存可能誤事,出現一些錯誤的數據。像股票類網站實時更新等,這樣的網站是不要緩存的,像有的網站很少更新,有緩存還是比較好的。今天主要介紹清除緩存的幾種方法。
清理網站緩存的幾種方法
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")
方法五:
window.location.replace("WebForm1.aspx");
參數就是你要覆蓋的頁面,replace的原理就是用當前頁面替換掉replace參數指定的頁面。
a.html
以下是引用片段:
<
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
以下是引用片段:
<
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
>
原文鏈接:https://www.jb51.net/article/108541.htm
js清除瀏覽器緩存的幾種方法