24

We've got the following situation, running from a single domain:

Page A uses window.open() to open a named window (a popup player). window.open() gives page A a reference to the window.

User now reloads page A. The reference to the named window is lost. Using window.open() to "find" the window has the unfortunate side effect of reloading it (undesirable). Is there any other way to get a reference to this window?

spender
  • 117,338
  • 33
  • 229
  • 351

6 Answers6

28

Try this:

var playerUrl = 'http://my.player...';
var popupPlayer= window.open('', 'popupPlayer', 'width=150,height=100') ;
if(popupPlayer.location.href == 'about:blank' ){
    popupPlayer.location = playerUrl ;
}
popupPlayer.focus();

It will open a blank window with a unique name. Since the url is blank, the content of the window will not be reloaded.

awe
  • 21,938
  • 6
  • 78
  • 91
  • This looks great. Will try when I get a chance. – spender Mar 16 '10 at 15:29
  • 2
    Instead of popupPlayer.location I recommend using popupPlayer.location.href. This avoids problems when using === instead of == (e.g. CoffeeScript does this automatically). – Philipp Sep 08 '14 at 22:10
  • There can be issues if the URL of the popup window is not within the same domain as the current window. Wrap the IF in a try/catch to avoid issues. – Derek Wade Sep 24 '16 at 08:57
1

AFAIK, no there isn't..

A kind-of-dirty-but-i-guess-it-will-work hack would be to periodically reset the reference on the parent window from within the popup using window.opener, with something like this code:


    setInterval(function() {
        if(window.opener) {
            window.opener.document.myPopupWindow = window
        }
    }, 100)

In the parent window, you'll be able to access document.myPopupWindow, even after a reload (well, 100ms after the reload). This should work cross browser.

Alexander Malfait
  • 2,691
  • 1
  • 23
  • 23
0

Actually what you did is destroy the parent (page A) of the created window (Popup), so it has no more reference to the original parent therefore you can't get a direct reference.

The only solution I can think of is using a browser that offers you added javascript capability to cycle through active windows (tabs) and find one that has a special property (ie: your reloaded page A) that gets recognized by the popup.

Unfortunately I guess only firefox has some added capability or extension that gives you this flexibility. (it is also a security risk though)

OverLex
  • 2,501
  • 1
  • 24
  • 27
0

This should work. Add this code in the popup:

function updateOpener() {
    if (window.opener)
        window.opener.document.myPopupWindow = window;
    else
        setTimeout(updateOpener, 100);
}

updateOpener(); 

And this in onload of the parent window. To make sure myPopupWindow have been set wait 100 ms before accessing it.

setTimeout(function() {
    if (document.myPopupWindow) 
        document.myPopupWindow.focus();
}, 100);
Johan
  • 29
  • 6
0

If all the windows share a common Url origin you can register a ServiceWorker and then access all windows from the ServiceWorker: https://developer.mozilla.org/en-US/docs/Web/API/Clients

AFAIK You won't be able to pass a reference to other windows from WorkerService to your window but you can establish communications with the ServiceWorker via

Gleba
  • 161
  • 1
  • 3
0

It Might help someone, If you opened an child tab and after refreshing the parent tab, you still want to focus on that child tab instead of opening new child tab: -

const chatPopup = window.open('', 'chatPopup');
if (chatPopup.location.href === 'about:blank' || !chatPopup.location.href.includes('/chat')) {
  this.openNewWindow = window.open('/chat', 'chatPopup');}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 01 '22 at 02:32