How to detect browser close or tab close event in chrome? - javascript

I am trying to detect browser or tab close event for Chrome, so that I can invalidate the session for my Java web application. I have tried onbeforeload() and unload(), but these functions are called when the content of page is unloaded within my web application. Also I am unable to override these functions in Javascript for Chrome browser.
Is there any way I can find browser close or tab close event for Chrome and make an ajax call and invalidate the session?

Related

Google Chrome Extension, closing Persistent background page

I have a chrome extension which has a persistent background page in order to stream audio even when the user closes chrome. What I would like is to implement an option for the user to disable audio streaming after chrome has been closed. Right now I am detecting when all chrome windows have been closed and pausing audio, however chrome remains 'open' in the task manager and system tray (note that running in background chrome setting is enabled). This stops audio playback but the chrome process remains open in task manger and the icon remains in the system tray. By contrast if I completely disable the extension and then close chrome, the application closes completely and no system tray icon remains.
This Question suggests that only a user can explicitly close chrome if there is a persistent background page, My goal isn't to close chrome explicitly but simply prevent MY extension from being the one keeping chrome open (depending on user specified options), therefore an event page doesn't seem to be a good fit.
Is there a function call or some other programmatic way I can close my persistent background page once all windows have been closed, if that option has been enabled? Or am I stuck with the chrome application staying open because the background page is persistent?
I've found an answer to my own question so I'll post it here. In order to have a background page you do not need the "Background" permission declared in your manifest. This permission is what allows the extension to stay open after chrome has closed (and potentially open before chrome?) rather than being necessary to have a background page. So a persistent background page will close with chrome if this permission is not set, and will stay open even after chrome is closed if it is set.
Bearing this in mind, the solution for me was to set the "background" permission to optional, and enable it if I wanted my extension to stay open after close, then remove the permission if I didn't want it staying open after close (the user now has an option to toggle this).

How to check if some IE window is already opened by javascript within that window

our web app doesn't support IE but customer wants to use call center functionality which is only supported in IE due to activeX control of call center.
As a work around, when user clicks on call button in chrome, web app opens the callCenter.html page in IE by using URL Alert protocol , and that callCenter.html page starts the initialization of activeX control for further operations. So far its OK.
Problem is, if callCenter.html window is already opened in IE, and user clicks again on call button in chrome, it opens another window of callCenter.html in IE, which results in activeX control initialization again and here it gets malfunctioning, since that activeX control is already loaded and running in previous IE window.
So please suggest, if there is some way, by which we can detect if callCenter.html window is already opened in IE, and if its already opened, it should update the calling parameters for call center.
Many thanks for your kind suggestions

Detect tab close on chrome mobile android browser

I tried beforeunload and onunload events but they get called only when user navigate to a different page via address bar not when user close the chrome android browser tab.
I want to send analytics code only when user has closed the page via tab also.

Chrome/Safari: how to open the same window from different tabs or windows

In a web application, I have an optional popup window into which logging content is being loaded. This window is opened via Javascript using window.open().
The users typically have multiple tabs of the application open and my intention is to always reuse the same popup window (if currently open), no matter from which tab they trigger the popup. To do so, I use code similar to this to open the popup:
<script>
function myFunction() {
var myWindow = window.open("popup.html", "my_popup", "width=600,height=400,status,resizable");
}
</script>
This works fine in Firefox and IE - only a single popup window is ever opened and all content that I want to log is being loaded into that window.
Chrome and Safari however, use different popup windows depending on the tab from which the popup is launched.
I assumed that the second parameter of window.open() specifies a target name from a global namespace, which seems to be the case for most browsers. But Chrome and Safari behave as if there were tab-specific namespaces for these target names, i.e. "my_popup" referenced from tab 1 refers to a different target than "my_popup" from tab 2. (to make things more complicated, tabs that have been "duplicated" seem to share the same namespace, i.e. they do reuse the same popup window, but tabs created otherwise don't.)
Is there a way to circumvent this behavior of Chrome and Safari and access the same popup window from all tabs that the user may have open?
Or do I have to assume that this is browser-specific behavior for which there seems to be no workaround?
I have tested this with various browsers:
IE 11.0.9600.18204
All tabs with content from the same domain open/reload a single shared popup window (i.e the target namespace seems to be local per domain).
All tabs with content from file:// URLs reuse the same popup window as tabs with content from http:// URLs that point to the local intranet.
Firefox 46.0.1
All tabs with content from the same domain open/reload a single shared popup window (i.e the target namespace seems to be local per domain).
All tabs with content from file:// URLs use their own single shared popup window (i.e. file:// URLs seem to have their own target namespace).
Edge 20.10240.16384.0
The behavior is inconsistent: in most of my tests, Edge behaved like Firefox, but occasionally like Chrome as well.
I don't have a problem in ignoring Edge for the time being.
Chrome 50.0.2661.94
Each tab opens or reloads its own popup window (i.e the target namespace seems to be local per tab)
Safari 9.1
Each tab opens or reloads its own popup window (i.e the target namespace seems to be local per tab)
By the way, in all of these browsers the behavior does not depend on whether or not the popup window was launched due to user interaction: the behavior is identical between cases where the popup is launched via body onload() and cases where it is loaded via button onclick().
[here's additional background information that I assume not to be relevant to the problem, but who knows... ;-)
Actually, our application does not directly open the popup windows, instead we're using log4javascript which opens a logging window (or in case of Chrome: multiple logging windows). I'm willing to extend the log4javascript code to deal with this and send a patch to the maintainer, but in order to do so I need to solve the basic problem described above]

Browser close event instead of tab close event

I have a asp.net page in which I want to update database when browser close not tab close. I tried window.onbeforeunload event in JavaScript. It is working good enough when browser tab close but not giving desire result when Browser close in firefox only (not IE). What should I do in this circumstance?

Categories

Resources