監聽瀏覽器的後退事件

1.window的popstate事件

監聽瀏覽器回退事件,主要依賴於監聽:window的popstate事件,由於瀏覽器在被點擊「後退」或者「前進"按鈕時,都會觸發popstate事件。
所以,能夠在javascript

window.on('popstate', function(){
    // to do
})

2.window的pushState事件

window的pushState事件,是實現往瀏覽器的歷史記錄中添加記錄,可是不刷新當前頁面的功能,即沒有發生任何請求致使window.location.href發生變化。此方法有三個參數,分別是:java

state:要新添加的記錄的狀態,是個對象
title:新頁面的title,能夠爲空
href:新頁面的url

3. 監聽瀏覽器後退的實現

3.1 將當前頁面的URL後面加
$(document).ready(function (e) {
        if (window.history && window.history.pushState) {
            $(window).on('popstate', function () {
                self.location="/credit/miniprogram/copartner/bankList"; //如查須要跳轉頁面就用它
            });
        }
        window.history.pushState('forward', null, '#'); //在IE中必須得有這兩行
        window.history.forward(1);
    });
3.2 將當前頁面壓入歷史記錄中
"pushState" in window.history && (
    window.history.pushState({
        title: document.title,
        url: location.href
    }, document.title, location.href),
      //setTimeout(function () {
          window.addEventListener("popstate", function (a) {
            self.location="/credit/miniprogram/copartner/bankList";                    
          })
      //})
    );

PS: 不是特別明白pushState的做用,這裏只是代碼的搬運工,記錄一下,後續修改吧。瀏覽器

相關文章
相關標籤/搜索