监听关闭浏览器触发事件

 关闭和刷新页面都会触发,一般都不用来做弹窗提示,一般用来做数据操作

// 监听页面关闭 清除本地缓存

 window.onbeforeunload = function (e) {

   localStorage.removeItem("statement");

};

 // 监听页面关闭 提醒是否关闭  现在不允许自定义内容了,默认是关闭浏览器不会保存操作

window.onbeforeunload = function(e) {
    console.log('beforeunload')
    return 1;
};

 

只有以下情况不会弹出确认窗格

  • 不return
  • return;
  • return undefined;
  • return null;

 return其他任何值都会阻止页面关闭

猜你喜欢

转载自blog.csdn.net/weixin_70563937/article/details/131248309