Question

I know this is almost the same question: ask by Joe

I have a web application. When I close the window (clicking X on browser) it will call the Logout functionality.

The problem is when I open the web application and open the same web application on different window (new window or another tab). And close one of the window it will call the Logout functionality even if there is still an open window for that application.

What I want to do is, check first if there are other window that is using the same jsessionid with the current window I am about to close. And when I close that window, It will only call the Logout functionality if there is no window using the same jsessionid.

Was it helpful?

Solution

The standard way of course would be to have the login cookie expire at browser close and thereby log you out, but I'm guessing this is not an acceptable behaviour in your case?

AFAIK you can't access the content of another browser window unless that window was created using Javascript. Since it sounds like you're using onUnload handlers in Javascript, you could make use of those same handlers to keep track of your windows. It would lead to some overhead though and would not be full-proof (would not handle browser crashes or if the user navigates away from your app for example).

Pseudo-code: (this needs to be a mix of server-side code and client-side javascript since the load handlers are handled 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();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top