Developer

Set a window variable to null

If you're working with JavaScript windows, you may need to set a windows variable to null at some point in time. Here's how.

By Brent Lee Metcalfe

Let's get what may seem like the most trivial tip—but the most important tip overall—out of the way. Before you declare a variable for a window open() method, declare it as a global variable and set it to null. Something similar to this:

<html>
<head>
<script language="JavaScript">
<—
var windowVariable = null;
function launchWindow() {
  windowVariable = window.open();

// window conditions and statements go here

}
//—>
</script>

If you don't implement this simple tip, crucial windowing code will fail. Setting a global variable to null lets you query the value of a corresponding local variable before the local variable has (or no longer has) a value.

Brent Lee Metcalfe is a Web architecture consultant.

Editor's Picks

Free Newsletters, In your Inbox