onpageshow事件和onload事件。 onpageshow 事件類(lèi)似于 onload 事件,onload 事件在頁(yè)面第一次加載時(shí)觸發(fā), onpageshow 事件在每次加載頁(yè)面時(shí)觸發(fā),即 onload 事件在頁(yè)面從瀏覽器緩存中讀取時(shí)不觸發(fā)。
為了查看頁(yè)面是直接從服務(wù)器上載入還是從緩存中讀取,你可以使用 PageTransitionEvent 對(duì)象的 persisted 屬性來(lái)判斷。 如果頁(yè)面從瀏覽器的緩存中讀取該屬性返回 ture,否則返回 false
解決方案
通過(guò)onload方式
代碼如下:
頁(yè)面中寫(xiě)一個(gè)隱藏的input
input type= hidden id= refreshed value= no
js操作如下
onload=function(){ var refreshedId=document.getElementById( refreshed if(refreshedId.value== no ){ refreshedId.value= yes } else{ refreshedId.value= no location.reload(); }
通過(guò)onpageshow 方式
這種方式在電腦上是沒(méi)有問(wèn)題的,但是蘋(píng)果safari中返回不執(zhí)行onload事件,要用如下方式:
window.onpageshow = function(event) { if (event.persisted) { window.location.reload() };
通過(guò)實(shí)際操作發(fā)現(xiàn),event.persisted在電腦中一直是返回false,但是在手機(jī)safari中是沒(méi)有問(wèn)題的。
綜合解決方案
因此,可以如下寫(xiě)代碼:
if ((/iphone|ipod|ipad.*os 5/gi).test(navigator.appVersion)) { window.onpageshow = function(event) { if (event.persisted) { window.location.reload() }else{ onload=function(){ var refreshedId=document.getElementById( refreshed if(refreshedId.value== no ){ refreshedId.value= yes } else{ refreshedId.value= no location.reload(); }
通過(guò)上面代碼發(fā)現(xiàn)在safari中第一次打開(kāi)頁(yè)面的時(shí)候,有時(shí)候會(huì)出現(xiàn)閃屏效果。
添加如下代碼:
$(window).bind( unload , function() { });
就沒(méi)有再出現(xiàn)閃屏效果了。
通過(guò)iframe方式阻止緩存
頁(yè)面中添加如下代碼
iframe >這種方式有待驗(yàn)證。
通過(guò)時(shí)間戳強(qiáng)制刷新方式
下面代碼是針對(duì)iPad中safari返回按鈕問(wèn)題
var showLoadingBoxSetIntervalVar;var showLoadingBoxCount = 0;var showLoadingBoxLoadedTimestamp = 0function showLoadingBox(text) { var showLoadingBoxSetIntervalVar=self.setInterval(function(){showLoadingBoxIpadRelaod()},1000); showLoadingBoxCount = 0 showLoadingBoxLoadedTimestamp = new Date().getTime(); //Here load the spinnerfunction showLoadingBoxIpadRelaod() //計(jì)算時(shí)間超過(guò)500毫秒 var diffTime = ( (new Date().getTime()) - showLoadingBoxLoadedTimestamp - 500)/1000; showLoadingBoxCount = showLoadingBoxCount + 1; var isiPad = navigator.userAgent.match(/iPad/i) != null; if(diffTime showLoadingBoxCount isiPad){ location.reload();}相信看了這些案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注php 其它相關(guān)文章!
相關(guān)閱讀:
CSS的編碼怎么轉(zhuǎn)換
css3點(diǎn)擊顯示漣漪特效
怎樣用canvas做出粒子噴泉?jiǎng)赢?huà)的效果
以上就是點(diǎn)擊瀏覽器的返回按鈕,就會(huì)刷新歷史頁(yè)面這個(gè)問(wèn)題的解決方案的詳細(xì)內(nèi)容,html教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選