Flynn81
Flynn81

Reputation: 4168

Is there anyway to trap the closing of a web browser?

Two years ago I had a need to trap the closing of a web browser as a javascript event for a web app. At the time I found there was no way doing this that worked for all browsers. IE had an event that I could used, but it was IE specific. I looked into other work arounds, like a heart beat sort of ping to the server, but I didn't like any of them at the time.

Is there anyway currently to trap the closing of a web browser now? Thanks!

Upvotes: 4

Views: 779

Answers (2)

Jesse Taber
Jesse Taber

Reputation: 2386

You can use the

window.onbeforeunload 

javascript event to do this, though this will trap more than just closing the browser. This event will also get fired each time someone tries to navigate to another page, refresh the current page, etc. It's handy if you're trying to do something like warn people of unsaved changes before they leave the current page.

Upvotes: 9

17 of 26
17 of 26

Reputation: 27382

onunload works in IE and Firefox.

Upvotes: 2

Related Questions