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

180
Views
Unable to detect when closing browser tab in Angular

I am trying to show some confirmation box when user is going to close the browser window using angular but as per my code its not working. My code is given below.

app.component.ts:

import { AfterViewInit, Component, OnInit, ElementRef, HostListener } from '@angular/core';
@HostListener('window:beforeunload', ['$event'])
  beforeunloadHandler(event) {
    if(confirm("Are you sure to close ")) {
      console.log("Implement close functionality here");
    }
    //return false;
    //I have used return false but you can your other functions or any query or condition
  }

Here I need when user will close tab on browser this confirmation box should appear but the above code I could not get any such alert. If anybody has any new solution please help to resolve this issue.

about 4 years ago · Juan Pablo Isaza
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!