spacer
Yehuda Shiran May 6, 2000
Sniffing Window Existence
Tips: May 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

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 = "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.


People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS · Sending an HTML and Plain Text E-newsletter with ASP.NET, Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Kingston Debuts Power-Saving Memory Upgrades · Web Marketing Tools: What's Your Reputation? · Ecommerce Marketing Tips: Writing Better Copy