0

I want to trigger event on page refresh.

I try all these methods but none of them worked. When I reload the page, nothing happens. What can I try to resolve this?

//1
@HostListener('window:unload', ['$event'])
unloadHandler(event: any) {
  alert(1)
}
//2
@HostListener('window:beforeunload', ['$event'])
beforeUnloadHandler(event: any) {
  alert(2)
}
//3
window.onbeforeunload = function(event)
{
    return confirm("Confirm refresh");
};
halfer
  • 19,471
  • 17
  • 87
  • 173
levi
  • 807
  • 2
  • 10
  • 20
  • Does this answer your question? [window.onbeforeunload not displaying the alert box](https://stackoverflow.com/questions/20067472/window-onbeforeunload-not-displaying-the-alert-box) – JSON Derulo Dec 23 '21 at 13:04
  • Thanks on the response! it's also dose nothing. maybe it's issue in angular? – levi Dec 23 '21 at 13:23
  • The question I linked just shows that the browser blocks alerts in `beforeunload` events. So in order to test if it's working, you should not use alerts. What exactly are you trying to achieve? – JSON Derulo Dec 23 '21 at 14:44

1 Answers1

0

Why are you not using the ngOnDestroy() callback on the AppComponent? Your code will be triggered when the app is unloaded.

pierreavn
  • 565
  • 5
  • 15