[thelist] javascript detecting if function/var is already defined

Simon Willison cs1spw at bath.ac.uk
Thu Nov 27 11:53:25 CST 2003


christof.hoeke at e-7.com wrote:

> hi, i have a problem with a script which depends on a function x()
> which may be defined or not, depending on the page. normally i (at
> least i think it worked) tried to find out if x is available by
> checking
> 
> if (x == "undefined") //do something other, maybe calling function y
> but not x else //do something with x, e.g. call it
> 
> this does not work though (although i am quite sure it worked at one
> time). i always get a browser error "x is not defined"

You need this:

if (typeof x == 'undefined') {
   // do something
}

Cheers,

Simon Willison



More information about the thelist mailing list