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.

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.