Last Comment Bug 165418 - |window.opener.closed| errors if the window's opener is already closed
: |window.opener.closed| errors if the window's opener is already closed
Status: NEW
Product: Core
Classification: Components
Component: DOM: Core & HTML (show other bugs)
: Trunk
: x86 Windows 98
-- normal with 3 votes (vote)
: ---
Assigned To: Nobody; OK to take it and work on it
: Andrew Overholt [:overholt]
Depends on:
  Show dependency treegraph
Reported: 2002-08-29 04:18 PDT by Bud
Modified: 2009-08-22 20:52 PDT (History)
6 users (show)
See Also:
Crash Signature:
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Description User image Bud 2002-08-29 04:18:16 PDT
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 User image Ian Neal 2002-08-29 06:14:04 PDT
What build IDs have you tested this against?
Comment 2 User image Phil Schwartau 2002-08-29 11:22:58 PDT
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.
Comment 3 User image Bud 2002-08-29 16:01:58 PDT
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 = '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 User image Phil Schwartau 2002-08-29 17:59:34 PDT
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 ) {


Comment 5 User image Phil Schwartau 2002-08-29 18:01:48 PDT
typo above:  supposed to use the test the |closed| property
should be    supposed to test the |closed| property
Comment 6 User image Boris Zbarsky [:bz] (still a bit busy) 2002-08-29 18:53:41 PDT
So... why the use of .closed?  This lives on nsIDOMWindowInternal, so it's very
definitely not for public consumption...
Comment 7 User image Johnny Stenback (:jst, 2002-09-03 19:43:43 PDT
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).
Comment 8 User image Johnny Stenback (:jst, 2003-03-23 13:38:54 PST
Mass-reassigning bugs to
Comment 9 User image martin gosling 2003-05-17 16:13:36 PDT
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.
Comment 10 User image Boris Zbarsky [:bz] (still a bit busy) 2003-05-17 16:21:00 PDT
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 User image Ho KS 2003-06-08 23:35:50 PDT
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 User image Ralf Vitasek 2004-01-19 09:26:15 PST
this seems the be the same problem i'm having atm.
see the URL i added ( ).
(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.

Note You need to log in before you can comment on or make changes to this bug.