H5 應用程式返回button的js代碼設計,設計仿stack

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.