Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
ASP.NET: How to detect user hitting cross to close window versus Postback in modern browsers

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.)

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda