3

I need to call a method through ajax whenever the user try to close the browser or tab. From the docs I came to know that we can achieve this through window.onbeforeunload(chrome). But this is called whenever I click on back,forward buttons or by hitting the refresh button. Is there any possible way so that I can exclude these actions and trigger it only if the browser is closed.

Below is the code snippet I am using

window.onbeforeunload = function (event) {
 var message = 'Important: Your will be logged out.';
 if (event) {
  $.ajax({
   ...
  });
 }
 return message;
};
gmuraleekrishna
  • 3,375
  • 1
  • 27
  • 45
Ashik
  • 91
  • 7
  • @anquegi I referred these before and it was triggering the back, forward and refresh events too. I just need to catch only the close event Thanks :) – Ashik May 23 '16 at 06:54

0 Answers0