How to write window closing event handler in Angular 2
, by that I mean closing not refreshing.
So I can't use window.onBeforeunLoad();
So you can use the visibilityState with the beforeunload event to differentiate whether the user refreshed or closed the tab.
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.
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.
Show activity on this post. $(document). ready(function() { $(window). bind("beforeunload", function() { return confirm("Do you really want to close?"); }); });
Try like this :
import { HostListener } from '@angular/core';
@HostListener('window:beforeunload', ['$event'])
beforeUnloadHander(event) {
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With