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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.