Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find window previously opened by window.open

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?

like image 421
spender Avatar asked Mar 16 '10 14:03

spender


People also ask

What does window open return?

When window. open() returns, the window always contains about:blank . The actual fetching of the URL is deferred and starts after the current script block finishes executing. The window creation and the loading of the referenced resource are done asynchronously.

How do you check if a window is already open in JavaScript?

How to check if an opened browser window is closed or not in JavaScript? To check if an opened browser window is closed, you can use the closed property in referenced window object in JavaScript. The property returns a boolean true if the window is closed and false if the window is in the opened state.

How do I return a value from a Windows Open?

Typically the onclick event on the "Yes" or "Ok" button in the modal dialog looks like this: window. returnValue = true; window. close();


2 Answers

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.

like image 127
awe Avatar answered Sep 26 '22 20:09

awe


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.

like image 36
Alexander Malfait Avatar answered Sep 22 '22 20:09

Alexander Malfait