Wednesday, 5 May 2010

How to prevent Browser Close



First, some clarifications of my requirements:

  1. To prevent the user from closing browser or tab (both are treated similarly).
  2. Note that browser close is different from browser exit. Browser close is done using the "X" button, while browser exit is done using File>Close or Alt+F4 keys.
  3. JS can only catch browser close event.
  4. URL refresh was also to be treated as close.
  5. However, navigation using anchor, buttons or any other DHTML was to be ignored.
  6. Tested on IE8 and Firefox 3.5.