Is it possible to determine if a named window is open in JavaScript?

I am working on a one-site cross-site project, and I had a little problem on my lap. When the user leaves the "parent" site, a specific page needs to be loaded into a pop-up window containing the "child" site. However, I cannot save the link to the return value of window.open(…) , because the user needs to allow navigation at his discretion on each site before logging out.

It would be easy if I could assume that the child site is always open, and another window.open(…) in the same window will change its URL. However, a pop-up window cannot appear if it is not already open (not all users have access to a child site).

I think this gives me two conflicting scenarios. When a user visits a sub site:

  • The user is registered on the parent site.
  • User clicks link for sub site
  • The children's site appears in a window with the name "child_popup".
  • The user browses all the pages on the parent site, forgetting that the child window exists.
  • The user logs out of the parent site.
  • A pop-up window for the child is redirected to the exit page from the child site.

And when the user cannot or cannot visit the subsidiary site:

  • The user is registered on the parent site.
  • The user views the data on the parent site far and far.
  • The user logs out of the parent site.
  • A popup should not appear!

So my limitations are:

  • I can’t store the link to the window in JS, as the user can navigate through any number of pages between visiting the subsidiary site and logging out.
  • I have zero control over the contents of the subsidiary site (my employer launches the parent site).
  • A parent site cannot be converted to frames to provide a "global" JS scope. :-)

I could not find any relevant resources on Google or SO. Is there any way to do this?

+4
source share
4 answers

We were unable to find a way to determine whether the window of the subsidiary site remains open, but we came up with a workaround that satisfied the needs of our business:

  • Set cookie A from the parent site when the popup starts.
  • Set cookie B from the sub site each time the page loads.
  • Clear cookie B from the sub site on each page.
  • Upon exiting the parent site:
    • If cookie A is set, clear it and close the local connection to the child site.
    • If cookie B is set, clear it and open the exit page from the child site in a pop-up window.
+1
source

Yes, you can, subject to one important limitation.

It depends on the following behavior:

  • the first 2 parameters of window.open are the URL and window name
  • window.open returns a link to a window
  • If the window is already open with the specified name, a link to this window is returned, and not a link to a new window
  • If the URL is NULL, an existing window will not navigate a new page

This means that you can get a link to an existing window without losing the page opened by that window. However, if the window does not exist with this name, it will open.

We used this approach at http://carbonlogic.co.uk/ , but due to a Flash Player error, the contents of the pop-up window are not working properly at the moment

+3
source

Use sessions to save popup state.

When the user clicks the link to open the sub site, you need to make an asynchronous javascript call to the parent server, which will record this user by opening a window on the sub site. OR open a link on a page that stores the following session information and returns a Location header.

(Note that I am using php, $ _SESSION [...] is a user-defined 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 asynchronous javascript call, or by logging out of the script.

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

Then display the exit child window. Make sure you turn off the session variable when you log out.

Warrior, profit.

0
source

It looks like you control the contents of the child window ... If so, you can try setting "window.opener.some_attr = true" the first time you load the child window.

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

0
source

All Articles