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

christof.hoeke at e-7.com christof.hoeke at e-7.com
Fri Nov 28 02:48:52 CST 2003


Von: Simon Willison [mailto:cs1spw at bath.ac.uk]

> 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


hi Simon,
thanks for this, so there even seem to be 2 solutions...

best regards
chris


More information about the thelist mailing list