1

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,824
  • 17
  • 99
  • 186
levi
  • 1,077
  • 2
  • 11
  • 24
  • 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
  • 607
  • 6
  • 16