Determining whether an orphaned child window is still open

Here's a good one for you.

I need a way to determine whether an orphaned popup window is still in existence, after the calling page has reloaded (or changed pages).

Oh, and the solution can't use other popup windows or "background" documents, and cookies are not an option.

What is he talking about?
------------------------------

Try this:
index1.html creates a popup window called "pop1" using a standard window.open() call. It assignes a reference to this new window in a variable. As long as we stay inside index1.html, we will be able to do stuff to the popup window by referencing the variable we assigned the instance to.
Now, from index1.html, link to index2.html. As humans, we know that pop1 still exists, but index2.html has reloaded and no longer contains the reference to the child window.

Now we can re-establish the connection to the child window from index2.html by trying to open a new popup window with the same name and an empty URL - window.open ("", "pop1"); This is great if the window does indeed exists. We have restablished the connection, and interestingly, pop1 is not reloaded. That works great. Problem is, if pop1 does not, in fact, exist, then we have this stupid popup window that appears. Sure we can close it immediately after, but you still have a really suspect popup that appears and disappears. People will suspect something fishy.

So, how to determine whether pop1 is in existence without getting an annoying popup if it is not? And without using background windows, additional popups or cookies.

Look forward to your (tested!) suggestions.

Tom
LVL 14
tomaugerdotcomAsked:
Who is Participating?
 
TheGDConnect With a Mentor Commented:
Tom, finally I got the solution for your problem, it took me 2 days to think on it :) First I would explain it then actual javascript code would follow.

You hava two domains, let say http://www.Domain1com and http://www.Domain2.com.

Website at Domain1 creates a popup which contain the url residing at Domain2.

Now this Domain2 url which in the form of a popup would contain an inline Hidden IFrame loaded with a special url from Domain1. Now the situation is: There is an popup displaying Domain2 url which contain a Domain1 url inside i.e. inside a Hidden IFrame.

Now in the popup window this Domain1 url Inside the Domain2 url CAN communicate 100% with Domain1 url who has opened this Popup through:
       parent.opener.(here any property or function of the opener window with Read/Write access)

Following is the javascript code for this Mechanism to work:

You will create 5 files as follows, 4 on Domain1 and 1 on Domain2 (popup contain Domain2 url):

******************************** START *****************************************************

***************************************************
***************** DOMAIN 1 ********************
***************************************************

**************** POPUP.JS ********************
var win = null;
var popup_status = 0;

function callFromPopup(msg) {
       popup_status = msg;      
}

function popup(url) {
       win = window.open(url, "pop1");
}

function CheckPopupExist() {
       CheckPopupExistActual(1);
}

function CheckPopupExistActual(v) {
       if (v == 1)
              setTimeout("CheckPopupExistActual(0);", 1000);
       else {
              //Check if popup exist
              if (popup_status)      
              alert("There is an popup window");
       else
              alert("There is no popup window");
       }
}
***************** INDEX1.HTML *****************
<html>
<head>
       <script language="javascript" src="popup.js"></script>
</head>
<body>
       <a href="index2.html">Click Here</a> to Go to index 2
       <br><br>
       <input type="button" value="Create Popup Window" onClick="popup('http://www.DOMAIN2.com/popup.html');">
       <br><br>
       <input type="button" value=" Check Popup Exist?    " onClick="CheckPopupExist();">
</body>
</html>
**************** INDEX2.HTML *****************************
<html>
<head>
       <script language="javascript" src="popup.js"></script>
</head>
<body>
       <a href="index1.html">Click Here</a> to Go to index 1
      
       <br><br>
       <input type="button" value="Check Popup Exist?" onClick="CheckPopupExist();">
</body>
</html>

**************** DOMAIN1MESSENGER.HTML *************************
<html>
<body onUnload="GoodBye()">
       This is inside popup. This text cant be seen as inside a hidden IFrame.
</body>
</html>

<script language="javascript">

function SignalToMainWindow(v) {
       parent.opener.callFromPopup(v);
}

function GoodBye() {
       clearInterval(intervalID);
       SignalToMainWindow(0);
}

var intervalID = setInterval("SignalToMainWindow(1)", 500);

</script>

***************************************************
***************** DOMAIN 2 ********************
***************************************************

***************** POPUP.HTML *****************
<html>
<body>
      This is popup.
      <iframe src="http://www.DOMAIN1.com/Domain1Messenger.html" style="display:none;"></iframe>
</body>
</html>

******************************** END *****************************************************

Instruction to setup the files:

1. Create DOMAIN1 files in your domain 1 (POPUP.JS, INDEX1.HTML, INDEX2.HTML, DOMAIN1MESSENGER.HTML).
2. Create DOMAIN2 file in your domain 2 (POPUP.HTML).
3. In INDEX1.HTML change http://www.DOMAIN2.com to your actual domain 2 address.
4. In POPUP.HTML change http://www.DOMAIN1.com to your actual domain 1 address.

Thats it.

How it is working:
==============

How does INDEX2.HTML OR INDEX1.HTML afrer reloading know that a popup exist or not? The answer is simple. The POPUP.HTML has DOMAIN1MESSENGER.HTML inside it. This is signalling the main window twice in a second that I am open or not, so the main window would always know if there is a popup or not.

SIMPLY you have a function CheckPopupExist() who will check for you if a popup exist or not.
CheckPopupExistActual() is the fnction where you would place your instruction that you want in-case a popup exist or not.
0
 
TheGDCommented:
It is possible only if you have access to the code of the url opened in popup window, i.e. if you can put some javascript there. So in your situation do you have access to the code of the url openend in popup window? If yes then I can assist you further towards the complete solution.
0
 
tomaugerdotcomAuthor Commented:
GD, I do have access to the code, in terms of being able to script whatever I want in either window.

BUT, please note that these two windows are in different domains, and so they cannot communicate with each other. They can write to each other's window.location, (but not read). So you cannot periodically push the child window name back to the parent window, if that's what you had in mind.

I look forward to your comments.

Tom
0
 
TheGDCommented:
Tom,

Do inform me if my solution works for you. Although I have tested it on my own domains.

Thanks.
0
 
tomaugerdotcomAuthor Commented:
Sorry it took me so long to get you the points. This solution doesn't work for me because IFrames are only IE and I need this to be cross-browser. Still, it's an excellent workaround and you certainly deserve the points, if only for your patience!

Thanks again.

Tom
0
All Courses

From novice to tech pro — start learning today.