Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Remote Window Close Notification in Firefox and IE

Reply
Thread Tools

Remote Window Close Notification in Firefox and IE

 
 
GEL
Guest
Posts: n/a
 
      06-21-2005
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.

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      06-21-2005
GEL said:

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


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

 
Reply With Quote
 
 
 
 
Jc
Guest
Posts: n/a
 
      06-21-2005
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".

 
Reply With Quote
 
GEL
Guest
Posts: n/a
 
      06-21-2005
*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

 
Reply With Quote
 
Jc
Guest
Posts: n/a
 
      06-21-2005
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.

 
Reply With Quote
 
GEL
Guest
Posts: n/a
 
      06-22-2005


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close a TCP socket? (TCPSocket#close doesn't close it) Iñaki Baz Castillo Ruby 7 01-12-2010 01:32 PM
close all child windows when close the main window jrefactors@hotmail.com HTML 6 08-07-2008 09:10 AM
close current window using window.close() Shang Wenbin Javascript 10 09-02-2005 12:16 AM
close all child windows when close the main window jrefactors@hotmail.com Javascript 3 01-16-2005 10:06 PM
Self.close() window without the dialog close?Yes/NO Walter Beierdonck Javascript 1 04-30-2004 02:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57