How to find out if there is another window using the same JSESSIONID?

I know this is almost the same question: ask Joe

I have a web application. When I close the window (by clicking X in the browser), it will call the "Exit" function.

The problem is that I open the web application and open the same web application in another window (in a new window or in another tab). And close one of the windows that will call the "Exit" function, even if there is an open window for this application.

What I want to do is first check if there is another window that uses the same jsessionid with the current window that I am going to close. And when I close this window, it will only call the Logout function if there is no window using the same jsessionid.

+4
source share
1 answer

The standard way, of course, would be for the cookie to expire when the browser closes and thus log out, but I assume this is not acceptable behavior in your case?

AFAIK, you cannot access the contents of another browser window if this window was not created using Javascript. Since it looks like you are using onUnload handlers in Javascript, you can use the same handlers to keep track of your windows. This would lead to some overhead, although it would not be full (it will not handle browser crashes or if the user navigates from your application, for example).

Pseudo-code: (this should be a combination of server-side code and client-side javascript, as load handlers are processed in Javascript and the session is server-side)

function OnLoad() { if (document.referrer != "{identify your app here}") Session("BrowserWindowsOpen")++; } function OnUnLoad() { if ({your code for if window is closed}) { Session("BrowserWindowsOpen")--; if (Session("BrowserWindowsOpen") == 0 ) performLogOut(); } } 
+3
source

Source: https://habr.com/ru/post/1437360/


All Articles