0

I want to be able to run a certain piece of code ONLY when the page is exited. I have tried this:

window.onbeforeunload = function(){
    new Notification('Window unloaded');
    return true;
}        

However, if the user presses 'cancel,' then the notification still shows up. How can I make it so that the code only runs if the page is fully exited?

  • Service workers. – Mechanic Jan 17 '21 at 19:22
  • 1
    Does this answer your question? [Run JavaScript code on window close or page refresh?](https://stackoverflow.com/questions/13443503/run-javascript-code-on-window-close-or-page-refresh) – KetZoomer Jan 17 '21 at 20:12
  • No, that does not answer my question. Whenever reload the page, if I press "Cancel", then the code still runs (I get a notification). I want the code to only run when the page is fully unloaded (exited). –  a person Jan 18 '21 at 22:02

0 Answers0