Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write window closing event handler in angular 2?

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

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

like image 429
Sruthi Varghese Avatar asked Nov 07 '17 06:11

Sruthi Varghese


People also ask

How to detect browser or tab closing in Angular?

So you can use the visibilityState with the beforeunload event to differentiate whether the user refreshed or closed the tab.

How to check if browser window is closed?

To check if an opened browser window is closed, you can use the closed property in referenced window object in JavaScript. The property returns a boolean true if the window is closed and false if the window is in the opened state.

How to handle window close event in JavaScript?

A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.

How to check browser close button is clicked in JavaScript?

Show activity on this post. $(document). ready(function() { $(window). bind("beforeunload", function() { return confirm("Do you really want to close?"); }); });


1 Answers

Try like this :

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

@HostListener('window:beforeunload', ['$event'])
beforeUnloadHander(event) {
    return false;
}
like image 188
Chandru Avatar answered Oct 21 '22 05:10

Chandru