JS——浏览器关闭事件

Posted cnloop

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS——浏览器关闭事件相关的知识,希望对你有一定的参考价值。

onbeforeunload

  • 关闭或刷新浏览器会触发此事件
window.onbeforeunload = function(ev) {       
    return true;
};

popstate

  • 当点击浏览器后退按钮会触发此事件

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

window.history.pushState("123", null, document.URL);
window.addEventListener("popstate", function() {
    _this.isMsgShow = true;
    history.pushState("123", null, document.URL);
});

以上是关于JS——浏览器关闭事件的主要内容,如果未能解决你的问题,请参考以下文章