Skip to content Skip to sidebar Skip to footer

Is It Possible To Determine If A Named Window Is Open In Javascript?

I'm working on an inter-site single-sign-on project and have had a pretty little problem dropped in my lap. When a user logs out of the 'parent' site, a particular page needs to b

Solution 1:

Yes, you can, subject to one important restriction.

It hinges on the following behaviour:

  1. the first 2 parameters of window.open are a URL and a window name
  2. window.open returns a reference to a window
  3. if a window is already open with the specified name, then a reference to that window is returned rather than a reference to a new window
  4. If the URL is NULL, an existing window won't navigate to a new page

This means that you can get a reference to an existing window, without losing the page that window has open. However, if a window doesn't exist with that name it will be opened.

We used this approach at http://carbonlogic.co.uk/ but because of a Flash Player issue the contents of the popup aren't working properly at the moment

Solution 2:

We weren't able to find a way to detect whether the child-site window is still open, but we came up with a workaround which satisfied our business requirements folks:

  1. Set cookie A from parent site when launching child popup.
  2. Set cookie B from child site on every page load.
  3. Clear cookie B from child site on every page unload.
  4. When logging out of parent site:
    1. If cookie A is set, clear it and close local connection to child site.
    2. If cookie B is set, clear it and open child logout page in popup.

Solution 3:

Use sessions to store the state of the popup.

When the user clicks a link to open the child site, you need do do an asyncronous javascript call to the parent server that records this user has opened a window to the child site. OR have the link open a page that stores the following session info and returns a Location: header.

(Notice I am using php, $_SESSION[...] is a user specific array of data stored between requests)

$_SESSION['inChildSite'] = true;

When the user logs out of the parent site, this value is checked again, either by an asycnronous javascript call, or by the logout script.

if ($_SESSION['inChildSite'] ==true ) 
     echo"<script>window.open(...)</script>"

Then display the logout child window. Make sure you unset your session variable when the logout happens.

Voila, profit.

Solution 4:

It sounds like you are in control of the contents of the child window... If so, you could try setting "window.opener.some_attr = true", when you first load the child window.

Thus your code in the parent window could do "if (window.some_attr) window.open(...)" or the converse "if (!window.some_attr) alert('no access')..."

Post a Comment for "Is It Possible To Determine If A Named Window Is Open In Javascript?"