0

hello I wondering is there any EVENTLISTENER exist in javaScript DOM that trigger right before the page reload not after. i was searching for a while and couldn't find any. I wanted to be sure so I asked it here thank for your time :)

reza
  • 27
  • 5
  • Does this answer your question? [Check if page gets reloaded or refreshed in JavaScript](https://stackoverflow.com/questions/5004978/check-if-page-gets-reloaded-or-refreshed-in-javascript) – Tân Aug 30 '20 at 08:54

1 Answers1

3

Is there any event listener exist that trigger before the page reload?

Yes, the window.beforeunload event.

The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. - MDN Definition

Keep in mind that as user JoshG commented, it will also fire on things such as page close.

Zach Jensz
  • 3,650
  • 5
  • 15
  • 30
  • 1
    This will get triggered before the page refreshes. But `beforeunload` will also be triggered when a user closes the page. So just be aware it might be a user closing the page and not a refresh. – JoshG Aug 30 '20 at 09:07