|window.opener.closed| errors if the window's opener is already closed




17 years ago
10 months ago


(Reporter: budingraham, Unassigned)


Windows 98

Firefox Tracking Flags

(Not tracked)




17 years ago
If I test to see if a window's opening window still exists by testing
window.opener.closed, and the opening window is already closed, this command
will hang the current window.

Comment 1

17 years ago
What build IDs have you tested this against?

Comment 2

17 years ago
Browser, not engine ---> DOM Level 0

We need a reduced testcase. Bud, if there is a small testcase,
could you paste the HTML here via the Additional Comments box?

The alternative would be to attach the parent and child HTML files
separately via the "Create a New Attachment" link above. But I'm 
guessing your testcase is small enough to paste the HTML for both
right here, in-line.
Assignee: rogerl → jst
Component: JavaScript Engine → DOM Level 0
QA Contact: pschwartau → desale

Comment 3

17 years ago
OK, here are the test files that you need.  This is
quite a simple test.  First using the
ParentWindow.html use the button to create the child
window.  Manually close down the Parent window once
the Child window is created.  Lastly, try to close the
Child window using the button on it.  The window will
not close - it will in IE6 and NS4.  Thanks for
looking into this.  Please also refer to the comments
that I made in the ChildWindow.html



<script language="Javascript">

function openChildWindow() {
	hWndAccess = window.open( 'ChildWindow.html', '','resizable,scrollbars,status' );



<form name="frmMain">

<input type="button" value="Open Child Window" onClick="openChildWindow();">




<script language="Javascript">

function closeDown() {
	// *** This 'if' statement will stop Mozilla in its tracks.
	// *** The window.close(); line below will never execute, 
	// *** nor will the alert come up.
	if( window.opener.closed ) {
		alert( 'I got here.' );
	// *** This following code will work in Mozilla, but not in IE6 or NS4
	if( !window.opener ) {
		alert( 'I got here.' );

	// *** The following code needs to be coded for all three to work.
	// *** Old code written before using just 'window.opener.closed'
	// *** just won't work until someone goes and recodes the pages.
	if( !window.opener || window.opener.closed ) {
		alert( 'I got here.' );


<form name="frmMain">

<input type="button" value="Close Child Window" onClick="closeDown();">



Comment 4

17 years ago
Following the steps to reproduce that Bud gave in Comment #3,
I get this error in Tools > Web Development > JavaScript Console:

        Error: window.opener has no properties
        Source File:file:///(path to)/ChildWindow.html
        Line: 11

I'm going to change the summary from:

    |window.opener.closed| hangs if the window's opener is already closed
to: |window.opener.closed| errors if the window's opener is already closed

because the browser doesn't hang. I'm still able to resize the browser
window, etc., and my CPU stays at 1-2%. 

The reason that you're not seeing the following alert() come up:

               if( window.opener.closed ) {
                   alert( 'I got here.' );

is not because the browser is hanging. It's because |window.opener|
is evaluating to |null|, hence the above syntax error when trying
to access |window.opener.closed| : |null| doesn't have any properties.

Once a run-time JS error occurs in Mozilla, all JS stops executing.
That's why the alert doesn't come up -

So it boils down to a question for the DOM folks: how are users 
supposed to use the test the |closed| property of |window.opener|?

Should it be done as Bud has indicated in his comments to 

         if( !window.opener || window.opener.closed ) {


Ever confirmed: true
Summary: window.opener.closed hangs if the window's opener is already closed → |window.opener.closed| errors if the window's opener is already closed

Comment 5

17 years ago
typo above:  supposed to use the test the |closed| property
should be    supposed to test the |closed| property
So... why the use of .closed?  This lives on nsIDOMWindowInternal, so it's very
definitely not for public consumption...
window.closed is a DOM0 property, so it *is* intended for public usage, at least
from JS (not sure why it lives in the _internal_ interface though, but so does a
lot of other DOM0 properties).
Mass-reassigning bugs to dom_bugs@netscape.com
Assignee: jst → dom_bugs

Comment 9

16 years ago
If, as Phil states, window.opener is evaluating to 'null' when the parent 
window is closed, why does (typeof window.opener) still evaluate to 'object'?

This is a most inconvenient bug. How else can we test if the opener is closed?

Is (!window.opener) a reliable alternative? What is (!null) anyway?

I'm using 7.02 on Windows XP.
typeof(null) is "object" in ECMAScript, as you could check directly in any browser.

(!null) tests true in ECMAScript (again, in all browsers).

Comment 11

16 years ago
not sure if it's related, but if the parent window changes location (eg. goes to
a new webpage), the child window loses all permissions to access (even read
permissions) the parent window's properties.
Is it possible to grant access to properties that don't threaten security?
(namely window.closed)

Comment 12

16 years ago
this seems the be the same problem i'm having atm.
see the URL i added ( http://www.tqsoft.de/playground/ ).
(when reloading the page you get the opener object alerted,
when closing the browser window a popup should appear that checks if the main
window exists, which fails in mozilla)

i tried to check window.opener.closed, but i get an error because the
window.opener object is killed when closing the window.
Assignee: general → nobody
QA Contact: desale → general

Move all DOM bugs that haven’t been updated in more than 3 years and has no one currently assigned to P5.

If you have questions, please contact :mdaly.
Priority: -- → P5
You need to log in before you can comment on or make changes to this bug.