views:

421

answers:

3

This has / may have been asked before, but, as far as I remember, the question was not satisfactory answered at that time ^^

How can I register a window or tab closing event with Javascript? I have tried body.onclose and body.onunload, and dozens others whose names I made up myself and thought they might possibly exist, but none of it worked, or, if it did, it only fired after the window or tab has been closed.

Question: Is there any way to register such an event before the window or tab has been closed? It needn't even be compatible with all browsers, as long as it works with Firefox.

Thanks in advance!

+2  A: 

window.onbeforeunload= …;.

KennyTM
YOU ARE THE BEST!
arik-so
A: 

WE don't have Onclose but we have OnbeforeUnload. Even I am looking for same event but no luck.

Rinkesh
A: 

window.onbeforeunload = function (e) { var e = e || window.event;

// For IE and Firefox if (e) { e.returnValue = 'Any string'; }

// For Safari return 'Any string'; };

Imtiyaz