Skip to content
Advertisement

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!

Advertisement

Answer

Not possible anymore.

Newer browsers doesn’t support it anymore.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement