Javascript: persist window object reference?
Asked Answered
T

3

6

My javascript code open some windows trough:

var win = window.open();

I store the win refernce in an array with all the other opened windows.
Everything works fine, until the opener is refreshed.

So what i'd like to achive is to get back all the references to the opened windows when the "master" window is loaded.

To realize this i have to:
1. persist all the references on unload 2. get back the references on load 3. update the references with the new opener

Is it possible? if so how?

Theft answered 24/9, 2010 at 15:45 Comment(0)
T
4

How I solved the issue.

The child window "opener" property keeps a reference to the WINDOW that opened it, so when the master window is refreshed the window doesn't change, so the opener reference is still the same. The only case when the opener is null is when the window is closed*

On the unload event of the master window, I call a javaScript function on the child window that sets a timeout. When the timeout ends I test the opener reference and try to register back because the reference is still correct.

This way I have all the child references back!! ;)

Of course, if the document loaded in the master window is not my document, but, for example, www.google.com registering back the child reference fails.

This solution has been teste under Firefox, IE and Chrome.

*in Chrome when you type a new address it start a new window, so opener will be null as well.

Theft answered 5/10, 2010 at 14:35 Comment(0)
R
5

This sounds very complicated (read: problematic).

What you're asking has been an issue since way back and goes back to the original HTTP protocol.

The short answer is no, this isn't really possible. When you refresh the parent all references to the children are lost. In all actuality it's quite realistic.... parent is "refreshed".... children get all the resources the parent left behind... sorry, off topic and well yea. The Web is stateless, so once a page is refreshed, reloaded, closed, opened, or had any other verb attached to it, the page is treated as a new document.


This works fine in chrome/FireFox, but does not work in IE.

Something like the following will work, but in order to reattach that relationship between child/parent it must be done FROM the child. The script/markup below will give an example. So I wouldn't use it, but it's here just as an example.

Steps:

  • Open Child
  • Go to Child
  • Refresh Parent
  • **Parent -> Child relationship is lost.
  • **Child -> Parent relationship still exists.
  • Link Parent
  • Go to Parent page
  • Check Relationship
  • **You will see the parent is now not null
  • Stay ON parent
  • Refresh Child

HTMLPage.htm:

<input type="button" onclick="openChild();" value="Open Child" />
<input type="button" onclick="refreshParent();" value="Refresh Parent" />
<input type="button" onclick="linkParent();" value="Link Parent" />
<input type="button" onclick="checkRelationship();" value="Check Relationship" />
<input type="button" onclick="refreshChild();" value="Refresh Child" />


<script type="text/javascript" language="javascript">

    var child;    
    function openChild() {
        child = window.open("HTMLPage.htm");
    }

    function refreshParent() {
        alert("refreshing parent");
        window.opener.location.href = window.opener.location.href;
    }

    function linkParent() {
        window.opener.child = window;
        checkRelationship();
    }

    function checkRelationship() {
        alert("Parent: " + window.opener + ".   Children:" + child);
    }

    function refreshChild() {
        child.location.href = child.location.href;
    }


</script>

So then my next question is:

What are you trying to achieve with this? I'm all for using a modal window / pop up window when the situation really calls for one, but do you need an array of them?

Does the parent page "need" to refresh? Could you stick an iframe on the parent page and refresh that (IE. file uploads etc. where the main page doesn't "need" to refresh).

I'm far from an apple fan boy, but this link does give a good insight on using IFrames with scripting: http://developer.apple.com/internet/webcontent/iframe.html

I hope this helps.

Rabah answered 24/9, 2010 at 16:10 Comment(0)
T
4

How I solved the issue.

The child window "opener" property keeps a reference to the WINDOW that opened it, so when the master window is refreshed the window doesn't change, so the opener reference is still the same. The only case when the opener is null is when the window is closed*

On the unload event of the master window, I call a javaScript function on the child window that sets a timeout. When the timeout ends I test the opener reference and try to register back because the reference is still correct.

This way I have all the child references back!! ;)

Of course, if the document loaded in the master window is not my document, but, for example, www.google.com registering back the child reference fails.

This solution has been teste under Firefox, IE and Chrome.

*in Chrome when you type a new address it start a new window, so opener will be null as well.

Theft answered 5/10, 2010 at 14:35 Comment(0)
L
0

either avoid refrehsing the main window (use AJAX or iframes), or put the "main window" into a frameset so that in fact only the "main frame" is reloaded - then you can stick the variables into the parent frame

Letterperfect answered 24/9, 2010 at 16:26 Comment(1)
when you have to resort to (javascript-controlled) popup windows, which are evil enough, framesets are not that more evil...Letterperfect

© 2022 - 2024 — McMap. All rights reserved.