监听用户点击浏览器后退按钮

var detectBack = {

        initialize: function() {
            //监听 hashchange 事件
            window.addEventListener('hashchange', function() {

                //为当前导航页附加一个 tag
                this.history.replaceState('hasHash', '', '');

            }, false);

            window.addEventListener('popstate', function(e) {

                if (e.state) {
                    //侦测是用户触发的后退操作, dosomething
                    //这里刷新当前 url
                    this.location.reload();
                }
            }, false);
        }
    }

    detectBack.initialize();

https://github.com/luokuning/blogs/issues/3

posted @ 2020-08-06 16:43  AlanTao  阅读(534)  评论(0编辑  收藏  举报