In my application i am allowing only 3 admin users at a time to log in to the application.
In case where one of the admin user directly closes the window or tab without signing out i need to invalidate the session or
need to decrement the number of admin users who has logged in.

I tried with following scenarios:

1. jQuery(window).bind("beforeunload", function(){
		//loadSrc('logout');
		//alert("You are about to leave the window");
		firstEntry = false;
		
		if(confirm("You are closing the window. do you want to continue. Click 'Ok' to close or click 'Cancel' to stay back")){
			loadSrc('logout');
			return;
		}else{
			loadSrc('appsListing');
			return;
		}
		//loadSrc('logout');
		  
	});
	
	2. onunload attribute in body tag and called the function loadSrc('logout').

Both of the above solutions failed to work when there are multiple tabs opened in the browser. But either of the above solution is working properly when the user closes the tab and not window.

Dani AI

Generated

— relying on unload/beforeunload alone will never be fully reliable across multiple tabs or browsers. The pragmatic solution is a hybrid: make the server authoritative (presence stored with a last-seen timestamp), use a best-effort client-side logout when possible, and coordinate tabs so a single browser session doesn’t double-count. ’s point about server timeouts is exactly the right fallback.

Recommended pattern (combine these three pieces):

  • Server-side presence + heartbeat. Each client updates a last-seen on the server every 20–60 seconds. The server treats an admin slot as active only if last-seen is within a TTL (for example, 90 seconds). That removes stale slots when a tab or browser crashes.

  • Best-effort unload signal. Use navigator.sendBeacon from an unload handler to request logout; it is more reliable for background send than synchronous XHR. Example:

    window.addEventListener('unload', () => {
      navigator.sendBeacon('/api/logout', JSON.stringify({ sessionId: SID }));
    });

    (Navigator.sendBeacon - MDN)

  • Cross-tab coordination so only the “last” tab actually asks the server to free the slot. Use BroadcastChannel (or localStorage/storage events as a fallback) to track open-tab IDs and only send the logout when no other tab remains open. (BroadcastChannel - MDN, StorageEvent - MDN)

Notes and limits: sendBeacon and unload are best-effort and can fail; always keep the server TTL as the source of truth. For real-time presence and immediate slot changes, use a WebSocket-based heartbeat so the server learns about closed sockets instantly. (WebSocket - MDN)

Recommended Answers

All 6 Replies

1) Java != JavaScript
2) Moved to JavaScript section

The above code snippet is in javascript only not Java

Hence why I move it from java section to JavaScript before you upset people with your sloppiness

Sorry about that as i am new to this site. Anyways thanks for your help.

The events you are looking for may be onunload & onbeforeunload (non-standard) events. However, these events are tricky and may not be able to achieve what you want. The reason is that these events would be fired when you leave the site via links or click on the "back" button.

The normal way to deal with session expiration is the timeout set at the server side. It is also a good way to teach users to "log out" instead of "close" the browser.

Thanks for your suggestion. Also i tried with window.onbeforeunload = function(){} which worked for me in single window but not in different windows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.