Remote Window Close Notification in Firefox and IE

Discussion in 'Javascript' started by GEL, Jun 21, 2005.

  1. GEL

    GEL Guest

    Hi, I want to open a new browser window, let the user use that window
    for several minutes, and when they close, I'd like to change the page
    displayed in the original window.

    According to numerous articles found Googling, this should work, but on
    my WinXP system, using Firefox and IE, I get nothing (when allowing
    pop-ups, if pop-ups are disabled, IE reports the window is closed,
    Firefox gives a JS error on checking the window handle). No JS errors,
    no notifications, nothing. Any pointers would be appreciated.

    File 1 contains the code I'm using to open the window, to check for
    closure, and a form textarea that I update with the time (mostly so I
    know my timer is firing properly).

    ---- FILE 1 BEG ----

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Opener Page</title>
    </head>
    <body>
    <h1>Opener</h1>
    <form name="frmOutput">
    <textarea name="txtOutput" rows=5 cols=80></textarea>
    </form>
    <p><a href="javascriptstopChecking();">Stop Checking</A></p>
    <script language="Javascript">
    log("Start", true);

    var remoteWin = window.open("Remote.html", "remote", 'toolbar=no,
    location=no, directories=no, status=yes, menubar=no, width=795,
    height=500, resizable=yes, scrollbars=yes, screenx=0, screeny=0, top=0,
    left=0');
    var timer = null;

    function checkClosed()
    {
    log("Checking...", false);
    timer = setTimeout("checkClosed()", 5000); // Check every 5 seconds.
    if (!remoteWin)
    {
    alert("Window no longer exists");
    stopChecking();
    }
    else if (remoteWin.Closed)
    {
    alert("Window Closed");
    stopChecking();
    }
    }

    timer = setTimeout("checkClosed()", 5000); // Check every 5 seconds.


    function stopChecking()
    {
    log("Stop checking.", false);
    clearTimeout(timer);
    }


    function log(sText, bClearContents)
    {
    var d = new Date();
    var s = d.getFullYear() + "." + d.getMonth() + "." + d.getDate() + " "
    + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + " " +
    sText + "\r\n";
    if (bClearContents)
    {
    document.frmOutput.txtOutput.value = s;
    }
    else
    {
    document.frmOutput.txtOutput.value = s +
    document.frmOutput.txtOutput.value;
    }
    }
    </script>


    </body>
    </html>


    ---- FILE 1 END ----

    File 2 contains some filler text. I can not change the source of this
    window (when live).
    ---- FILE 2 BEG (remote.html) ----

    <html><head><title>Remote</title></head><body><h1>Remote
    Window</h1><p>Closing me should alert the original window or allow the
    original window to know when I <a
    href="javascriptwindow.close();">close</a>.</p></body></html>

    ---- FILE 2 END (remote.html) ----

    TIA.
     
    GEL, Jun 21, 2005
    #1
    1. Advertising

  2. GEL

    Lee Guest

    GEL said:

    >else if (remoteWin.Closed)
    >{
    >alert("Window Closed");
    >stopChecking();


    Windows don't have an attribute named "Closed".
    Try checking remoteWin.closed
     
    Lee, Jun 21, 2005
    #2
    1. Advertising

  3. GEL

    Jc Guest

    GEL wrote:
    > Hi, I want to open a new browser window, let the user use that window
    > for several minutes, and when they close, I'd like to change the page
    > displayed in the original window.

    <snip>

    Assuming the page you are loading is from the same domain (and it
    appears to be), I would use events rather than polling. For example,
    after you open the remote window, you could use (untested):

    remoteWin.onbeforeunload = function() {
    if (opener && !opener.closed) {
    opener.location = "/new_url.htm";
    } //if
    }

    <snip>
    > else if (remoteWin.Closed)
    > {
    > alert("Window Closed");
    > stopChecking();
    > }

    <snip>

    I didn't look very closely at your code, but I did notice that you are
    checking for a property "Closed" on the window object, which should
    probably be "closed".
     
    Jc, Jun 21, 2005
    #3
  4. GEL

    GEL Guest

    *smack* <sound of hand slapping forehead>

    You nailed it with your "not so close" observation of .Closed !=
    ..closed. I didn't get an error in FireFox or IE, so assumed that was ok
    (I'm not used to the javascript lowercase letter first naming scheme,
    it's gotten me before).

    I changed my remoteWin.closed line and it works in both browsers. Will
    try the event method -- I like than much better, should be faster and
    less resource intensive.

    Does the remote window have to load from the local domain for this to
    work? If so, can I load a local page, that does a redirect
    (location.href) or at a lower server level?

    Thanks again for the information and for spotting the problem!
    --G
     
    GEL, Jun 21, 2005
    #4
  5. GEL

    Jc Guest

    GEL wrote:
    <snip>
    > Does the remote window have to load from the local domain for this to
    > work? If so, can I load a local page, that does a redirect
    > (location.href) or at a lower server level?

    <snip>

    You have to be able to access the DOM of the remote window to use that
    method, and this is subject to cross-frame security. Refer to:
    http://www.jibbering.com/faq/#FAQ4_19

    I think you are asking if you can load a dummy page (from the same
    domain) into the remote window, set the onbeforeunload event, and then
    redirect the remote window to a page from a different domain, and have
    the onbeforeunload event still fire when the remote window is closed
    while showing this new page.

    The answer is no, loading a new page (location.href, or other methods)
    unloads the old page and fires the event, which is then itself
    unloaded.

    If you want notification of a remote window being closed/unloaded that
    is from another domain, I am aware of a couple options:

    1. The technique you are currently using (polling from the parent
    window).
    2. Using a frameset or iframe in the remote window to allow a page from
    the same domain to also be loaded into the remote window (but not
    visible), on which you can set events. Since all frames get unloaded at
    once when the window is closed, this should have the same effect.

    I should also mention that if you only care about the remote window
    being closed, and you don't want notification when the user navigates
    the remote window to a new page (for example, if they click a link in
    the remote window), then you will probably want to use the polling
    technique or the frameset/iframe technique.
     
    Jc, Jun 21, 2005
    #5
  6. GEL

    GEL Guest

    Jc wrote:
    > GEL wrote:
    > <snip>
    > > Does the remote window have to load from the local domain for this to
    > > work? If so, can I load a local page, that does a redirect
    > > (location.href) or at a lower server level?

    > <snip>


    > If you want notification of a remote window being closed/unloaded that
    > is from another domain, I am aware of a couple options:


    > I should also mention that if you only care about the remote window
    > being closed, and you don't want notification when the user navigates
    > the remote window to a new page (for example, if they click a link in
    > the remote window), then you will probably want to use the polling
    > technique or the frameset/iframe technique.


    I only wanted to know when the window closed, I don't care if they go
    through one page or twenty. In "production", the user must complete 7-9
    pages before closing the window, but they may close sooner (I have
    already dealt with that).

    Using the window.onbeforeunload seems to work for local and remotely
    hosted pages in FireFox and IE6 (IE6 is only one client is concerned
    with at this point). The trick was to look for window.closed, not
    window.Closed.

    Thanks for all the quick responses.

    --G
     
    GEL, Jun 22, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    8,934
    sateeshranga
    Aug 7, 2008
  2. Iñaki Baz Castillo
    Replies:
    7
    Views:
    942
    Iñaki Baz Castillo
    Jan 12, 2010
  3. Walter Beierdonck

    Self.close() window without the dialog close?Yes/NO

    Walter Beierdonck, Apr 29, 2004, in forum: Javascript
    Replies:
    1
    Views:
    373
    Grant Wagner
    Apr 30, 2004
  4. Shang Wenbin

    close current window using window.close()

    Shang Wenbin, Sep 1, 2005, in forum: Javascript
    Replies:
    10
    Views:
    423
    Gérard Talbot
    Sep 2, 2005
  5. hyejin
    Replies:
    8
    Views:
    378
    Martin Honnen
    Aug 29, 2006
Loading...

Share This Page