I am currently detecting if a user of my site closes the window/tab or changes the url.
I am using the follwoing code, which works perfectly:
var validNa
The window close event is fired as the very last event. Therefore no other events are fired after it.
You can use the beforeunload event with Jquery, to show the overlay on screen.
The following link could help: link