8

How to write window closing event handler in Angular 2, by that I mean closing not refreshing.

So I can't use window.onBeforeunLoad();

Ankur Alankar Biswal
  • 1,184
  • 1
  • 12
  • 23
Sruthi Varghese
  • 577
  • 1
  • 6
  • 23
  • 1
    I think not any way to find differentiate browser closed or refresh. both are same in term of programming – Sandeep Nov 07 '17 at 06:30
  • Use host listeners for such a scope. – briosheje Nov 07 '17 at 06:35
  • its always better to give some thing you have tried , people her are not going to just solve them for you – Rahul Singh Nov 07 '17 at 06:48
  • 1
    You can refer this post https://stackoverflow.com/questions/34129319/how-can-i-handle-browser-tab-close-event-in-angular-only-close-not-refresh however it's not in `typescript` but you might get some idea or logic. – hrdkisback Nov 07 '17 at 07:48

1 Answers1

5

Try like this :

import { HostListener } from '@angular/core';

@HostListener('window:beforeunload', ['$event'])
beforeUnloadHander(event) {
    return false;
}
Chandru
  • 10,864
  • 6
  • 38
  • 53