標籤:
history.back();
該代碼具有天然的缺陷,二手知道,於H5應用,尤其是模仿行動裝置 App程式時,,這是不夠。
在放大期js為了實作類別似特徵,請輕噴。
不多說,上代碼:
/** * Created by [email protected] on 2015/5/28. */var historyUtils = { add : function (url) { var historyArray = historyUtils.getLocal(); if (!historyArray) { historyArray = []; } var currentPage = historyArray.pop(); if (currentPage && currentPage == url) { //do nothing } else if (currentPage){ historyArray.push(currentPage); //歷史裡面沒有如今傳入的url。在加回去 } historyArray.push(url); historyUtils.saveLocal(historyArray); }, back : function() { var historyArray = historyUtils.getLocal(); var currentPage = historyArray.pop();//去掉當前頁面,pop取最後,相似stack var history = historyArray.pop(); if (!history) {//沒有曆史頁面 historyUtils.add(currentPage);//將當前頁面增加回數組中 return; } historyUtils.saveLocal(historyArray); window.location.href = history; }, getLocal : function() { var result = window.sessionStorage.getItem(historyUtils.key); if (!result) { return null; } return JSON.parse(result); }, saveLocal : function(data) { window.sessionStorage.setItem(historyUtils.key, JSON.stringify(data)); }, init : function() { historyUtils.saveLocal([]); }, key : "_history_"}historyUtils.add(window.location.href);
在須要實現back的地方調用
historyUtils.back();
著作權聲明:本文博主原創文章,部落格,未經同意不得轉載。
H5 應用程式返回button的js代碼設計,設計仿stack