Search code examples
javascriptwindowobject-persistence

Javascript: persist window object reference?


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?


Solution

  • 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.