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

434
Visualizações
Handle Back button with Angular [Chrome]

I am creating a project using angular, In my application I need to handle back button event. Below is the code which is working fine apart from one scenario

Code:

this.location.subscribe(event => {
 //logout
});
history.pushState({}, '');

This code only works when user is perform some activity, if user just landed on page and click back button then this event is not captured. I tried all the ways but not works.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

A simple way is to add the popstate event, every time when user click on the back button. It will be fired.

window.addEventListener('popstate', (event) => {
    // The popstate event is fired each time when the current history entry changes.

    // logout or do any thing you like

}, false);

If this doesn't work you could go for Angular Router and check which event is called.

constructor(router: Router) {
    router.events
      .subscribe((event: NavigationStart) => {
        if (event.navigationTrigger === 'popstate') {
          // Perform actions
        }
      });
}
over 4 years ago · Santiago Trujillo Relatório

0

This solves the problem for me, captured even when no activity by user

@HostListener('window:popstate', [$event])
onPopState(event: any) {
  console.log('Event', event);
}

PS: tested in Chrome

over 4 years ago · Santiago Trujillo Relatório

0

You can try to override the whole onpopstate like this

window.onpopstate = function () {
  // Your logic should be here...
}.bind(this); // This line is to bind the components
over 4 years ago · Santiago Trujillo 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