On ZDNet: 10 most annoying programs

Set a window variable to null

Tags: Guest Contributor

  • Save
  • Print
  • 2

Takeaway: 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.

  • Save
  • Print
  • 2

What do you think?

advertisement
Click Here