更新時間:2022-07-15 09:55:03 來源:動力節(jié)點 瀏覽3542次
H5頁面緩存問題該如何解決?動力節(jié)點小編來告訴大家。
掃描二維碼進(jìn)入H5頁面,開發(fā)更新過H5頁面后此時的頁面并不是最新的,需要刷新一下才能更新
1.給地址添加一個隨機(jī)參數(shù),避免緩存
如:
先定義一個方法,獲取獲取路徑中的隨機(jī)參數(shù)
GetQueryString(name){ //獲取路徑中的參數(shù)
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return decodeURI(r[2]); return null;
}
在onLoad/頁面首次加載時調(diào)用,判斷頁面路徑中是否有隨機(jī)參數(shù),沒有的話加上隨機(jī)參數(shù),并重新加載,就不會有緩存了
onLoad() {
var random = this.GetQueryString('random'); // 獲取路徑中的的參數(shù)random
if (!random) { // 首次打開時是沒有random參數(shù)的,所以接下來地址加上參數(shù),然后重新加載,重新加載時已經(jīng)又random了就不會再執(zhí)行這里了。
location.replace(location.pathname + '?random=' + Math.random());
setTimeout(function () {
location.reload();
}, 300);
}
}
2.修改head塊
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
初級 202925
初級 203221
初級 202629
初級 203743