View Full Version : Validating a variable is declared

12-07-2006, 06:44 PM
I have a situation in javascript where (this is inherited code, so I gotta deal with it) a variable is being used without being declared, as

someVar = someExpression ;

The original developer was relying on IE which automagically creates javascript variables for the IDs of all HTML elements, so somewhere in the HTML, there's
<someTAG id='someVar' >blah </someTAG>.

Unfortunately, in Mozilla (Firefox), I get a javascript error indicating that 'someVar' is not defined.

I wanted to check to see if the variable was undefined, then define it
if (undefined == someVar) someVar = document.getElementById("someVar") ;

this gives the same error in Mozilla (undeclared variable). I saw a thread with this exact problem, but it looks like its been archived. Does anyone have the solution to checking in javascript if a name is "undeclared" as opposed to "undefined"???

12-07-2006, 07:45 PM
if(typeof someVar=='undefined') someVar=document.getElementById("someVar")

12-07-2006, 10:11 PM
Arg!!!! 'undefined', not undefined.... Gotta put on my glasses