[Javascript] Object Not Defined

Claude Schnéegans schneegans at internetique.com
Mon Nov 21 17:48:11 CST 2011


 >>I suppose I could send a string to a function then split the string on the . and then iterate through it to check for existence...

Obviously you can only pass the name of the object as a string, because if you pass the object like in exist(a.b.c.d.e.f), it must be passed by reference, and if the object does not exist, there is no reference and there will be an error before the function can be called. But there id no need to split and iterate (unless you want to know which first sub object is not defined).

Try this:
<script>
function exists(objectName)
   {
   return (eval(objectName) != null);
   }
alert (exists("document.location.doesNotExist"))
alert (exists("document.location.reload"))
</script>


More information about the Javascript mailing list