1. 程式人生 > >history.back()在safari中不重新載入js問題怎麼解決

history.back()在safari中不重新載入js問題怎麼解決

如果是ios的,先判斷出來,location.href定位回去

$("#backPrev").attr("href","javascript:void(0);").click(function(){
    if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) {             
            window.location.href = window.document.referrer;
    } else { window.history.go("-1"); }
});

判斷safari可以

 if(isSafari=navigator.userAgent.indexOf("Safari"
)>0) { return "Safari"; }

但是這樣會導致自帶的返回按鈕混亂,比如,從a頁面進入b頁面,然後b頁面搜尋進入c頁面,c頁面返回,進入b頁面,但是由於是連結,此時b頁面返回仍然是c頁面。

終極解決方法:

在跳轉頁面之前,改寫當前頁面地址

$("#backPrev").attr("href","javascript:void(0);").click(function(){
    if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) {
            changeCurrentUrl();             
            window.location.href = window.document.referrer;
    } else
{ window.history.go("-1"); } }); function changeCurrentUrl(){ var replaceUrl = window.location.href+"?i="+new Date().getTime(); history.replaceState(null,"",replaceUrl); }