1

I am trying to trigger a function in my vue.js app only when the user closes the app. I already found the possibility to register an event listener for beforeunload. Using this event, the function will also trigger when the page is just reloaded - which is not really what I want.

Can anybody tell me if there is a way to prevent beforeunload from triggering on refresh? Or is there a completely different way to fire a function whenever my page is closed?

Thank you in advance! :)

McMuellermilch
  • 95
  • 1
  • 10
  • 2
    Does this answer your question? [javascript beforeunload detect refresh versus close](https://stackoverflow.com/questions/11453741/javascript-beforeunload-detect-refresh-versus-close) – ChrisG May 17 '21 at 15:40
  • 1
    :- https://stackoverflow.com/a/3888938/11492378 – Toxy May 17 '21 at 15:42

1 Answers1

3

I don't think there is a way to differentiate between a refresh and a page close, but you can maybe set a timestamp in local storage and then check that timestamp on load and if it's close you can assume it was a refresh. The before unload will still run though, but maybe you can write some js to undo whatever the before unload did when it was a refresh.

Edit: This answer to a similar question may also be helpful: https://stackoverflow.com/a/14893469/5460296

jmona789
  • 2,711
  • 6
  • 24
  • 57