Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!