By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,147 Members | 744 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,147 IT Pros & Developers. It's quick & easy.

Remote Window Close Notification in Firefox and IE

P: n/a
GEL
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.

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Jc
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".

Jul 23 '05 #2

P: n/a
GEL
*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

Jul 23 '05 #3

P: n/a
Lee
GEL said:
else if (remoteWin.Closed)
{
alert("Window Closed");
stopChecking();


Windows don't have an attribute named "Closed".
Try checking remoteWin.closed

Jul 23 '05 #4

P: n/a
Jc
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.

Jul 23 '05 #5

P: n/a
GEL


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

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.