Find window previously opened by window.open

我的梦境 提交于 2019-11-27 19:25:16

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.

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.

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)

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!