移動端使用window.history.back()無法傳回上一頁,網上搜到的解決方法是
window.location.href = document.referrer;
但是如果是在兩個頁面來回切換幾次,傳回時就會陷入無限循環。
沒辦法,隻能自己動手了,自定義一套解決方案。
解決思路是把通路過的url放到數組裡面,再存儲到localStorage裡面,傳回時從裡面讀取最後一個url,讀取後再把最後一個url移除。具體實作如下
//曆史記錄
var jndx_currenturl = window.location.href;
var jndx_historyarr = new Array();
var jndx_historystr = "";
if(jndx_currenturl.indexOf("/ydsiteb/default/index")<0){
jndx_historystr = localStorage.getItem("jndx_historystr");
}
if(jndx_historystr.length>0){
jndx_historyarr = JSON.parse(jndx_historystr);
}
var jndx_lasthisurl = "";
var jndx_historynum = jndx_historyarr.length;
if(jndx_historynum>0){
jndx_lasthisurl = jndx_historyarr[jndx_historynum];
}
if(jndx_lasthisurl!=jndx_currenturl && jndx_historyarr.indexOf(jndx_currenturl)<0){
jndx_historyarr.push(jndx_currenturl);
}
localStorage.setItem("jndx_historystr",JSON.stringify(jndx_historyarr));
//傳回上頁
function gotoback(){
var jndx_prevurl = jndx_historyarr.pop();
if(jndx_prevurl==jndx_currenturl){
jndx_prevurl = jndx_historyarr.pop();
}
if(!jndx_prevurl){
jndx_prevurl = "/ydsiteb/default/index?randnum="+ Math.random();
}
localStorage.setItem("jndx_historystr",JSON.stringify(jndx_historyarr));
window.location.href = jndx_prevurl;
return false;
}
//傳回首頁
function gotohome(){
window.location.href = "/ydsiteb/default/index?randnum="+ Math.random();
return false;
}