dumbnoob Posted December 30, 2009 Share Posted December 30, 2009 Ok, I have a script, that when run, will cycle through the contents of an array, and when it reaches the end, will loop back to the beginning. However, if the variable used to determine its position has not been set on the page already, it doesn't run. I need a way to set the variable, only if it does not exist. My code below javascript: if (confpos==undefined){ confpos=0; } config=[1,2,4,7,12,16]; IT=(config[confpos]); alert (IT); confpos=++confpos; IT=(config[confpos]); if (IT==undefined){ confpos=1; } void(0); If I have previously punched javascript: confpos=0; void(0); into my address bar, the code will run fine, but, I need it to do what it's meant to, without me having to predefine confpos. In that, if confpos isn't set, then I need it to set it automatically, so that it will be able to run the rest of the script. Link to comment https://forums.phpfreaks.com/topic/186653-need-help-with-setting-setting-a-variable-only-if-it-doesnt-exist-already/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.