javascript关闭浏览器事件的方法

小编给大家分享一下javascript关闭浏览器事件的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

javascript关闭浏览器事件的方法:1、使用onbeforeunload方法,关闭或刷新浏览器会触发此事件;2、使用popstate方法,代码为【history.pushState("123", null, document.UR】。

本教程操作环境:windows7系统、javascript1.8.5版,DELL G3电脑。

javascript关闭浏览器事件的方法:

1、onbeforeunload

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

示例代码

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

2、popstate

示例代码

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

以上是“javascript关闭浏览器事件的方法”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!