0

I am trying to detect event when user are trying to close current tab or windows.I tried onbeforeunload and stack overflow answers. But no one worked for me. can any one help me to solve this. I followed this link to get answer but not worked for me. Below is the code which I tested on chrome.

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

 (e || window.event).returnValue = confirmationMessage; //Gecko + IE
   return confirmationMessage;                            //Webkit, Safari, Chrome
 });

Thanks.

1 Answers1

1

If I remember corretly, those "custom" conformation messages don't work anymore. There's only the brower's standard one left:

window.onbeforeunload = function(event) {
  event.preventDefault();
  return '';
};

Should work on this page when you click "Run code snippet".

CodeF0x
  • 2,624
  • 6
  • 17
  • 28
  • But it didn't work on firefox, I want that script which works on all modern browsers. –  Sep 06 '18 at 17:45
  • @Rajat For me, it worked in Safari, Chrome, and Firefox. What Firefox version do you use? – CodeF0x Sep 06 '18 at 17:49
  • @Rajat What comes to my mind right now; did you reload your page without the cache after putting this snippet into your code? Your old JS might still be cached, so you can't see the changes. – CodeF0x Sep 07 '18 at 06:56