Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q272 When a page is reloaded how can it check that any window it previously opened is not still open before attempting to open it again?

You are here: irt.org | FAQ | JavaScript | Window | Q272 [ previous next ]

Once the location of a page has changed, or the page has reloaded, it loses all knowledge about any windows it may have opened. Therefoe code to detect whether the popup window is still open will not work.

The only workaround is to use a frameset with a hidden frame and to open the popup window from the parent frameset. This way the reference to the popup window is preserved even if one of the frames is reloaded.

<HTML>
<HEAD>

<SCRIPT LANGUAGE="JavaScript"><!--
function openWindow() {
    if (!window.window2) {
        // has not yet been defined
        window2 = window.open('apage.html','windowRef','width=100,height=100');
    }
    else {
        // has been defined
        if (!window2.closed) {
            // still open
            window2.focus();
        }
        else {
            window2 = window.open('apage.html','windowRef','width=100,height=100');
        }
    }
}

openWindow()
//--></SCRIPT>

</HEAD>

<FRAMESET COLS="100%,*">
<FRAME SRC="apage.html">
<FRAME SRC="bpage.html">
</FRAMESET>

</HTML>

If need be you can then invoke the parents openWindow() function from one of the frames to reopen the window if you think it may have been closed, with: parent.openWindow().

the following was submitted anonymously:

I don't use frames, below is the code I use. The parent window can check to see if the daughter window is already open or don't allow the daughter window to lose focus "focus"...

In the parent window open the daughter window with the following code:

var daughterWindow = null;

function openBrowser(f,p,t) {
   //f = path to file
   //p = parameters
   //t = title - optional   this identifies the daughterwindow

   if (!daughterWindow || daughterWindow.closed) {
      daughterWindow = window.open(f,t,p);
   } else {
      alert("Close any other open daughter windows!");
   }
}

function handleSetFocusToDW() {
    //set this function to fire on the document.onfocus() event so daughter window can't loose focus

    if (daughterWindow) {
        if (!daughterWindow.closed) {
            daughterWindow.focus();
        }
    }
}

Feedback on 'Q272 When a page is reloaded how can it check that any window it previously opened is not still open before attempting to open it again?'

©2018 Martin Webb