Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture browser close event?

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.

like image 307
Lan Avatar asked Jul 08 '11 09:07

Lan


People also ask

How do I find close browser events?

A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.

How do you handle browser tab close angular not close refresh?

The best way I found to reload the page without losing cookies, and remove cookies on window or tab close was to use ng-keydown to watch the F5 Keypress (KeyCode 116).

How do I close a JavaScript browser?

JavaScript provides an in-built function named close() to close the browser window that is opened by using window. open() method.

What is Beforeunload event?

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.


1 Answers

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;
  }
};
like image 94
Praveen Prasad Avatar answered Oct 11 '22 15:10

Praveen Prasad