0

How to achieve Leave site alert using Angular when user try to close the browser

I tried using Hostlistener beforeunload and unload.

I paused by debugger i checked. It showing the below error the

1) Blocking Alert After beforeunload 2) Blocking Alert After unload

1 Answers1

0

You can use unload and beforeunload events to when you want to deal with this kind of issues.Below is the code will help you to get solution.

export class AppComponent {
  @HostListener('window:unload', [ '$event' ])
  unloadHandler(event) {
    //If you want to do something when page gets reloaded or on leaving page
  }

  @HostListener('window:beforeunload', [ '$event' ])
  beforeUnloadHander(event) {
    return false;
  }
} 

I hope you will get your solution.

Ishan Bhatt
  • 54
  • 10