Browser closing event

Browser closing event

I need to avoid the user closes the browser during an application or, bettere, I want to intercept the close action, do something and the close the browser.

I didn't  find the way.

Unload event is fired every time a page si unloaded but I'm not able to see if  the close button was pushed or the page changes for every other reason.

Could someone suggest a solution

Tks

Tullio