We have an app which is approaching 20 years old which relies on pop-up windows to edit user records, each of which has a session state object and relies on full postback ASP.NET c# code. The problem we're having (similar to I want to detect when user close browser window? but want to avoid necro-ing an 13 year old post) is that in non-IE browsers the solution we had to detect clicking the cross vs a postback no longer works. This relied on onbeforeunload and using event.clientY to check the mouse position (an ugly hack but the only thing that worked) which doesn't exist in e.g. Chrome.
The problem is it doesn't seem possible to distinguish clicking the cross versus doing a postback as both are treated as unloading the form. Is there a way to do this in modern browsers? We have tried the following (doing an async call to the server to flag that the session object needs to be removed):
onbeforeunload - pops up the 'changing site?' box on any postback action (as well as the cross).
onunload - fires on any postback as well as cross.
onvisibilitychange checking the - same as onunload. e.g.:
document.onvisibilitychange = handleWindowClose;
function handleWindowClose(e) {
var v = document.visibilityState;
if (v === "hidden") {
CallServer("Cancel"); // ajax call to server to close session
}
}
Checking IsPostBack / IsCallBack and using a flag in the session state itself to track the difference. Clicking a postback control will hit the server twice, once for the control event and then the ajax call which causes even more problems as code relying on the session state being there will break when its removed - we have over 200 pages to deal with so this isn't an option for us.
There is the option for a heartbeat mentioned in the 2009 post to check for dead window handles from the parent window but this won't work if that window is closed for example (we have windows opening other windows etc.)