How to detect if the browser window is closing (not reload, only close) in javascript

Viewed 1174

I need to call a function that logsout users from the system when the browser's window is closing.

I found a function window.onbeforeunload but this also gets called when you reload the browser and I dont want the user to be logged out when he reloads the browser. I need to do it once the browser window is closed.

Is this even feasible?

1 Answers

From Firefox Documentation

window.addEventListener("beforeunload", function (e) {
  var confirm_message = "\o/";

  (e || window.event).returnValue = confirm_message ; 
  return confirm_message ;                      
});

This example working on all browsers

Related