How does a referenced pop-up object change when a page is opened?

It drives me crazy!

Scenario:

A popup window opens on the main page, and then the function is called:

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

... some code later ...

 newWin.remoteFunction(options); 

and this is normal.

Now the popup is still open, and the main page changes to page 2. NewWin no longer exists, and I need to recreate the link to the popup window object to call the remote function again (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 () in the popup every time the open () method is called a navigation violation (I absolutely need a popup that works in the background).

The only workaround I can solve is to create an interval in the popup menu with:

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

and then set another spacing on the opening page with try / catch, but it is inefficient and very inefficient.

so, interestingly, is it really so difficult to get a link to pop-up objects between different pages in a window that opens?

+4
source share
1 answer

Not tested, just for the idea:

in the main window:

 window.onbeforeunload = newWin.sendYourself(); 

in the 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 the main window, New Page :

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

However, it is still too complex and not very effective.

Any better ways?

0
source

Source: https://habr.com/ru/post/1309566/


All Articles