On the client side, you can use onunload() (or onbeforeunload() to give "you are sure" that they would be grateful if it meant that they accidentally closed something after writing a long stackoverlow.com message, but would be annoying users in most cases).
On the server side you cannot, although you can force onunload to do something to signal the server.
As a rule, this means that you consider the Internet as a state, and not stateless. Stateless projects work better with stateless protocols such as web pages; the more your application considers each request as unique and independent of previous requests (although, of course, only that this request will depend on how the client dealt with the previous request), your application is more scalable and reliable.
Jon hanna
source share