Running a method on browser close tab or browser exit - javascript

Is there an action handler I can use in Javascript+Angular+JQuery that makes it possible to schedule a method to be executed just before a tab is closed in the web browser?
By close I mean quitting the browser or closing the tab or crashing or anything...

Yeah, you just bind the the beforeunload on the window object.
angular.element($window).bind("beforeunload", exitHandler)

Related

How to Differentiate between browser close and tab close

I want my application to execute some code when the browser is closed. My problem is beforeunload event that is called on both the browser close button and tab close. I don't need that code to be executed on tab close. Is there any way that we can execute that code on browser close not on tab close?
I have tried beforeunload event that is calling on browser close and tab close.
I need to separate these close calls so that the same code shouldn't be executed on for browser close and tab close.
Unfortunately it is not possible to differentiate between these events. Refer to this thread Detect browser or tab closing

JavaScript browser close event (nob tabs)

I know that onbeforeunload and unload events are triggered while a user closes the browser tab or the browser itself, so my question is:
Is there any way to detect only browser close (not tabs), like you completely destroy your browser process?
As per my research on this subject, I found out that as of October 2017, there is no way to handle full browser close event in JavaScript.

How can I handle browser tab restore?

The browser tab is closed and then restored using Ctrl+Shift+T.
I need to execute some logic at the moment when tab is closing or is restored.
But I shouldn't handle navigation to other page, so events like beforeunload are not ok.
It's guaranteed that application is not open in more than one tab.
How can I implement this?
PS: Same question in Russian.

How to get idle time of a window.open() window?

As soon as the page loads, a window pops open using
var pop_window = window.open("third party site url".......)
My requirement is to -
1) close the window when the popup window is inactive.
My question - the window can be closed using pop_window.close(). but how to check for inactivity of the window when I only have the handler "pop_window"?
You can't you have to have the javascript you need in the new window you open, and if its different domain then you totally can't control or even close,
and even window.close() is not working well for all browsers, it may work for Chrome but not for Firefox, Try it cross browsers before you use in production.

Close window from browser history

I am working on a web application where I am using JavaScript for the client side scripting. Now my requirement is to close all the opened window which were opened through window.showModalDialog().
For this, I read the history of the browser using window.history.length, but I do not know how to close each window. This works well for window.open(), but not for window.showModalDialog().
Could you please guide me to move forward?
You can close the opened window as follows:
To Open:
var window1=window.open("http://somedomain.com");
var window2=window.open("http://someotherdomain.com");
To Close
window1.close();
window2.close();
But be sure you call window1.close() and so on.. on the same script where you opened it.
You should not be using window.showModalDialog. Firefox has deprecated it, and Chrome has removed it. Also take a look at window.showModalDialog: What It is and Why You Should Never Use It.
The idea of showModalDialog is that all scripts are paused while the modal window is open. Thus, using window.close doesn't work, since as long as the window is open, no more scripts are being executed.
One possibility is to have JavaScript in the modal dialog so that is closes itself. You will not be able to close it from outside.

Categories