views:

114

answers:

1

This is driving me crazy!

Scenario:

The main page opens a pop-up window and later calls a function in it:

newWin = window.open(someurl,'newWin',options);

...some code later...

newWin.remoteFunction(options);

and it's ok.

Now, popup is still open and Main Page changes to Page 2. In Page 2 newWin doesn't exist anymore and I need to recreate the popup window object reference in order to call again the remote function (newWin.remoteFunction)

I tried something like:

newWin = open('','newWin', options);
if (!newWin || newWin.closed || !newWin.remoteFunction) {
    newWin = window.open(someurl,'newWin',options);
}

And it works, I can call newWin.remoteFunction again BUT Safari for some reason gives Focus() to the popup window everytime the open() method is called breaking the navigation (I absolutely need the popup working in the background).

Only workaround I can think to solve this is to create an interval in the popup with:

if(window.opener && !window.opener.newWin) window.opener.newWin = self;

and then set another interval in the opener Page with some try/catch but it is inelegant and very inefficient.

so, I wonder, it's really so hard to get the popup object reference between different pages in the opener window?

A: 

Not tested yet, just for the idea:

in Main Window:

window.onbeforeunload = newWin.sendYourself();

in PopUp window:

function sendYourself() {
    sendingMyself= setInterval('sendMyself',50);
}
function sendMyself() {
    if(window.opener && !window.opener.newWin) window.opener.newWin = self;
}
function okGotIt() {
    clearInterval(sendingMyself);
}

In Main Window, New Page:

window.onload = newWin.okGotIt; //just to make sure we have it...

However it's still too complex and not so efficient.

Any better ways?

achairapart
somehow it works...
achairapart