浏览器后退事件的方法

当页面已经填了内容,点击浏览器返回按钮,弹出确认框,是否退出?如果填写内容为空,则直接退出

注:以下是写在vue的methods里的代码

 window.addEventListener('popstate', function () {
                if (self.isEmpty) {
                    window.location.href = `http://www.baidu.com`;
                } else {
                    if (bool) {
                        window.history.pushState('forward', null, '');
                        window.history.forward(1);
                    }
                    self.cancelFlag = true;
                    document.body.style.overflow = 'hidden';
                    document.querySelector('.wrap').addEventListener('touchmove', this.preventDefault);//当弹出框弹出当时候,阻止页面滑动
                }
            });

preventDefault(e) {
e.preventDefault();
},

posted @ 2017-06-27 21:46  尋壹米陽光  阅读(1188)  评论(1编辑  收藏  举报