May 6, 2000 - Sniffing Window Existence
![]() |
May 6, 2000 Sniffing Window Existence Tips: May 2000
Yehuda Shiran, Ph.D.
|
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 = "http://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 = "http://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.


Find a programming school near you