You cab try the body onunload event. But u don’t think that triggers when the user closes the browser or kills the browser process.
Unless you have a strong reason to do that, I would stay away of it. This is just how the web works and it’s likely better to work with it rather than against it.
Check this site for example code: