Jump to content

Need help with setting setting a variable only if it doesnt exist already.


dumbnoob

Recommended Posts

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.