JS——浏览器关闭事件

onbeforeunload

  • 关闭或刷新浏览器会触发此事件

  • 示例代码

    window.onbeforeunload = function(ev) {       
        return true;
    };
    

popstate

  • 示例代码

    window.history.pushState("123", null, document.URL);
    window.addEventListener("popstate", function() {
        _this.isMsgShow = true;
        history.pushState("123", null, document.URL);
    });
    
  • 当点击浏览器前进或者后退按钮会触发此事件

  • 基本上不用这个事件,vue 框架可以帮助代替这个事件

posted @ 2018-07-21 18:04  cnloop  阅读(11087)  评论(0编辑  收藏  举报