View Single Post
Old 01-31-2012, 03:42 PM   PM User | #4
mjy
New Coder

 
Join Date: Jan 2012
Location: United States
Posts: 28
Thanks: 0
Thanked 6 Times in 6 Posts
mjy is an unknown quantity at this point
Although I don't understand the details of what you're doing, it seems that your basic problem is that in JavaScript you can't reference a non-existent data item (e.g. pass it to an 'exists()' function) without triggering a runtime error. One workaround you might experiment with is to preface the object name with 'window.'. Provided that you're working in a browser environment, global variables are actually properties of the global 'window' object. Referencing a non-existent property of an object merely returns 'undefined' and doesn't generate a runtime error. The following global code (not inside a function) illustrates this point:
Code:
var x = 5;

/* displays 5: */
alert (x);

/* also displays 5: */
alert (window.x);

/* y has NOT been declared, i.e. it doesn't exist: */

/* just displays "undefined": */
alert (window.y);

/* causes a runtime error: */
alert (y);
__________________
Author of the Kindle book JavaScript: Just the Basics - A Primer for the Complete Beginner. Learn JavaScript for the price of a fancy coffee drink.
mjy is offline   Reply With Quote
Users who have thanked mjy for this post:
TheIntern (01-31-2012)