0

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!

4
  • Does this answer your question? Run JavaScript code on window close or page refresh? Commented May 28, 2021 at 22:44
  • @HereticMonkey, I have already tried this. Commented May 28, 2021 at 23:19
  • I don't see anything in your question about trying the visibilitychange event as described in this answer... Commented May 28, 2021 at 23:47
  • @HereticMonkey, it still doesn't work. Commented May 29, 2021 at 1:03

1 Answer 1

1

Not possible anymore.

Newer browsers doesn't support it anymore.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.