Test if a variable exists

var foo = "bar";

if ( typeof( window[ 'foo' ] ) != "undefined" ) {
   document.write("foo is defined")
   } 
else {
   document.write("foo is undefined")
   }
Here is the result for "foo" :

And for fubar :




If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2010
[ home ]