How to capture browser close event?

browser, events, javascript, jquery

Solution

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;
  }
};

Problem

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.

Original source

Related problems