473,408 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Remote Window Close Notification in Firefox and IE

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
5 6911
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fogwolf | last post by:
First a basic outline of what I am trying to do: I want to have a page spawn a pop-up when you click "submit" on its form. On this pop-up page there will be another form. When you click "submit"...
4
by: Csaba Gabor | last post by:
Up until a few weeks ago, javascript code like window.open("http://mydomain.com", "windowName"); would always bring my new or reused window to the top, with focus. Lately, Firefox (Deer park...
1
by: Jake Barnes | last post by:
I've a button that onclick calls a function that calls the following code. The last 3 lines are for IE, which didn't want to close till I put in the funny line with opener. ...
4
by: Andre | last post by:
With Firefox 1.5+, I used the following code to close a window/tab: === <html><head> <script language="javascript" type="text/javascript"> function closeWindow() { window.open('','_parent','');...
4
by: badaczewski | last post by:
The following javascript appears on a popup window. <script language="javascript" type="text/javascript"> function InsertContact(value) { window.opener.CallBackContact(value); window.close();...
6
by: Howard Rifkin | last post by:
Hi, I my Javascript I popup an authentication window using the command; unamePasswdWindow = open("auth.html","","width=500,height=100"); This works fine in IE, and in Firefox 2.x on...
6
by: Bob Altman | last post by:
Hi all, This is a long shot, but I figure it doesn't hurt to ask... I have some applications that work fine when I run them on my PC at work, but "fail to initialize (0x000005)" when I'm...
29
Frinavale
by: Frinavale | last post by:
I have 2 FireFox (version 2) browser windows opened. One is the child of the other. When the user is finished with the child window, a method in the parent window is called to refresh a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.