如何捕获浏览器关闭事件? [英] How to capture browser close event?

查看:116
本文介绍了如何捕获浏览器关闭事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉的浏览器关闭事件在我的应用程序,并显示一个确认框用户。
我使用JSF 2.0和4.0 RichFaces的


解决方案

  window.onbeforeunload =功能()
{
  变种shallIAlertUser = Do_Whatever(); //获取boolen值
  如果(shallIAlertUser){
    //这将提醒用户
    返回'你确定吗?';
  }
  其他{
    //这不会
    window.onbeforeunload =不确定;
  }
};

I want to capture the browser close event in my application and show a confirm box to user. I am using JSF 2.0 and richfaces 4.0.

解决方案

window.onbeforeunload = function () 
{
  var shallIAlertUser = Do_Whatever(); //get boolen value
  if (shallIAlertUser) {
    //this will alert user
    return 'Are you sure?';
  }
  else {
    //this wont
    window.onbeforeunload = undefined;
  }
};

这篇关于如何捕获浏览器关闭事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆