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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint