Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 772
Flynn81 Avatar asked Dec 22 '22 12:12

Flynn81


2 Answers

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.

like image 149
Jesse Taber Avatar answered Dec 30 '22 23:12

Jesse Taber


onunload works in IE and Firefox.

like image 42
17 of 26 Avatar answered Dec 30 '22 23:12

17 of 26