Working with Windows: Checking if a Window Exists | WebReference

Working with Windows: Checking if a Window Exists


Working with Windows

Checking if a Window Exists

When you create a new window, it's important to assign the value returned by the open() method to a variable. For example, the following statements create a new window and immediately close it:

win = window.open("https://www.docjs.com/", "js");
win.close();

The window Object

Each browser window is associated with an explicit window object. Therefore, when you want to refer to the current window (the one containing your script), you should use the window object. The following statement sets the URL of the current window:

window.location.href = "https://www.docjs.com/";

When you put such a statement in a script, you don't need to specify the window object, because the existence of the current window is assumed:

location.href = "https://www.docjs.com/";

Note that self is equivalent to window, so self.close() is actually the same as window.close().

When you want to handle a window, you must make sure it still exists. Before you declare a variable for a window.open() method, declare it as a global variable and set it to null. Remember that the open() method returns the window object of the new window. Here's an example:

var win = null;
function launchWindow() {
  win = window.open();
  
  // statements that refer to the new window go here
}

If you want to perform an operation on the new window, you should first check if the variable win isn't null:

// if win exists, move the window
if (win) win.moveTo(0, 0);

Note that null evaluates to false, while any other valid object evaluates to true. If win evaluates to true, you know for sure that it isn't null, meaning that a new window was successfully launched.

The open "Property"

So you know the browser actually created the new window. But does it still exist? Not necessarily. You need to make sure the window variable holds a real window object. Since every window object features an open() method, you can detect the method via object detection:

// if win.open exists, move the window
if (win.open) win.moveTo(0, 0);

The variable win reflects the window object of the window, so win.open references the window's window.open() method. Notice that the conditional expression is a function reference (without parentheses), not a function call. You shouldn't attempt to evaluate win.open unless you know for sure that win exists. The following statement demonstrates the correct implementation:

// if win and win.open exist, move the window
if (win && win.open) win.moveTo(0, 0);

Since && is a short-circuit operator, it only evaluates its second operand (win.open, in this case) if the first one (win) reflects a true value. If the first operand evaluates to false, the entire expression is obviously false, so JavaScript doesn't even look at the second operand. This is an important behavior, because the expression win.open generates an error if win doesn't exist.

The closed Property

Browser windows have been a very difficult issue since the first version of JavaScript. For example, some methods of a window object, such as close(), can be executed even if the window has been closed, while others can't (e.g., moveTo()). Doesn't make any sense, does it? But things are even worse. Internet Explorer and Navigator often behave differently. Furthermore, it is sometimes hard to predict the result of a certain operation even though you've already done the same thing before. We'll show you how to overcome these problems by introducing a robust cross-browser statement that checks if a given browser window is open.

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 isn't 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).

Next: How to close a window

https://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: April 10, 2000
Revised: April 10, 2000

URL: https://www.webreference.com/js/tutorial1/reference.html