JQuery offload event
Browsers cannot distinguish between closing tabs, closing the browser, refreshing, entering a new address. Everything will trigger an unload event - the DOM is unloaded. Unfortunately, no data is passed to the offload event handler to differentiate between different causes.
You can do something with the position of the mouse - determine if the pointer is in the upper middle (for the address bar) or in the upper right / left corner for closing. However, its a great solution, since different browsers have their buttons in different positions, and Mac / PC have close buttons on opposite sides.
source to share