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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

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:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business