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

271
Visualizações
How to handle window unload event with browser back support in angular?

I have an web application which by default saves the user's data at server side if the user's connection get closed by any way which was not initiated by user (Like user's browser process get killed, user's system turned off or if user's internet get disconnected for some time). But if user himself closes the browser or closes the browser tab then in this case the server should delete the user's data at backend.

Basically I want to differentiate between when user himself intentionally closes the web page window or when he was get disconnected by any technical issue.

For this I have used the window's unload event and called an api to delete data.

  @HostListener('window:unload', ['$event'])
  unloadHandler($event: any) {
    this.sendApiToDeleteData();
  }

But this function is not getting called when clicking back button from browser. Since going back will be initiated by user by clicking on browser back button. I want to delete user data. But seems like unload does not works with back button.

Also, from documentation here. It says event is not compatible with the back/forward cache And since the doc also suggested not to use unload event. Is there any other way to handle unloading of page or any better way to handle my use case?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Hello there you can use this along with your code to detect back press:

  @HostListener('window:popstate', ['$event'])
  onPopState(event) {
    console.log('Back button clicked');
  }
about 4 years ago · Juan Pablo Isaza Relatório
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