Can I detect when the browser is about to close down (not just the window) and have code that does something before shutting down?

Jayesh Nazre

The best you can do is watch for the unload even.

NOTE: This piece of code is executed even if u refresh the page.Because when u refresh the page , the browser paints a new screen so the unload event gets fired.

This event gets fired in IE as well as NS6 (Mozilla), in NS4+ it does not work. However in NS4+ when u refresh the page it gets fired, but when you close the browser it does not.

<script language=javascript>
function cleanUp() {
	alert ("I AM CLOSING");
<body onunload=cleanUp()>