How to run a method when the window closes in JavaScript



I’m wondering how I can run a method when a window or tab closes. I’ve tried using the following…

window.onclose = () => {
  // execute function
}
window.onbeforeunload = () => {
  // execute function
}

but both of these only fire when I refresh the page not when I close it.

Any help would be very appreciated thanks!

Answer

Not possible anymore.

Newer browsers doesn’t support it anymore.



Source: stackoverflow