May 13, 2000 - The window.closed Property | WebReference

May 13, 2000 - The window.closed Property

Yehuda Shiran May 13, 2000
The window.closed Property
Tips: May 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

The window object's methods are not consistent in that some of them can be executed even if the window has been closed, while others can't. A typical example for the first type is the close() method. The moveTo() method, on the other hand, cannot be executed if the window has been closed. The surest way to check for an open window is to test the window.closed property. The window.closed property is a Boolean value that specifies whether a window has been closed. When a window closes, the window object that represents it continues to exist, and its closed property is set to true.

Use closed to determine whether a window that you opened, and to which you still hold a reference (from the return value of window.open), is still open. Once a window is closed, you should not attempt to manipulate it. Since window.closed is only supported by Internet Explorer 4, and Navigator 3, and later, you should account for previous versions. We'll use the following code:

// if win and win.open exist, and win.closed is not true, move the window
if (win && win.open && !win.closed) win.moveTo(0, 0);

Internet Explorer 3 and Navigator 2 don't support the closed property, so it evaluates to false in a Boolean expression (like any property that doesn't exist, such as window.tomershiran).

Learn more about windows in Tutorial 1.