Page 1 of 1

Variable variables in javascript

Posted: Mon Jan 12, 2009 8:18 am
by steelcity_ballin
Came across a problem I've never had to solve before, namely the title of this post. I have a page that is mostly dot net (vb) and in it I'm using a javascript framework to do some nice menu animations and what not, mostly it's just a window that responds when a radio button is pressed. Now the problem is, t he number of radio buttons is dynamic, or rather, should be dynamic based on a parameter vb has. I have no problems getting said parameter into javascript (I can alert it, for example), but what I want to do isn't working.

I want to create a loop that creates a variable for the number of something selected on a previous page. So if the parameter in vb comes in as 5, I should have 5 variables. I'm essentially writing code that should write code. I used a for loop and eval but no dice. YES I know eval is awful and evil, but the other methods I googled including dot notation and square brackets do not work. Here was my idea that does not work to demonstrate the effect I want:

<script language="javascript" type="text/javascript">
   var numProps = <%=numProps%>;   
   
   for(i=1;i<numProps+1;i++){
      eval("prop" + i) = "testing";
      alert(prop1)
   }


So let's say my initial variable numProps = 5, I should get the variables prop1, prop2, prop3, prop4, prop5 all with a value of "testing".

Re: Variable variables in javascript

Posted: Mon Jan 12, 2009 8:43 am
by steelcity_ballin
I fixed it myself.

var numProps = <%=numProps%>;   
   
   for(i=1;i<numProps+1;i++){
      eval('prop' + i + ' = "testing"');
      alert(prop1)
   }


Gah... so simple... :oops:

Re: Variable variables in javascript

Posted: Thu May 14, 2009 5:15 am
by rimmazz
How is a cookie that is assigned a value created in JavaScript? I've been reading about JavaScript cookies. But I am still not sure of a few things. I need a cookie, created in Java, that takes a variable that had been set already in another Java Script, and places it into a cookie that is good for the session only. document.cookie
seems like the the way to do this in Java, but I am confused about the rest, especially how to use it with a variable...
____________________
yahoo keyword tool ~ overture ~ traffic estimator ~ adwords traffic estimator

Re: Variable variables in javascript

Posted: Thu May 14, 2009 5:42 am
by steelcity_ballin
First I would want you to be sure that you mean to use JavaScript and that Java and Javascript are not the same thing at all. Second, creating the cookie is a rather trivial matter, Just google that, however to have the cookie good only for the session, you can set the cookie expiration date to a time in the past. I'm not sure I fully understand why you can't use a variable to set a cookie value. It would be no different than creating a cookie with a static value, simply supply the variable as the value.

If you can be a bit more descriptive in what you mean to do, I might be able to be more helpful.

Re: Variable variables in javascript

Posted: Thu May 14, 2009 10:28 am
by UberGerbil
I know this is way late, but
steelcity_ballin wrote:
Gah... so simple... :oops:
why aren't you using an array for this?

Re: Variable variables in javascript

Posted: Thu May 14, 2009 1:43 pm
by steelcity_ballin
I solved my problem just fine earlier, I was addressing the other guys question. Arrays are inefficient for what I want and don't server to do anything but further obfuscate the code. What I wrote works and is, all things considered, lightweight and elegant, easy to follow. If you follow my codeblock, it has the number of props set from VB, and even though I never explicitly instantiate a prop1 variable, It alerts and works given the eval.

Re: Variable variables in javascript

Posted: Thu May 14, 2009 8:26 pm
by titan
I think there should be a thread split here.

Cookies are pretty well covered with an appropriate Google search.

Re: Variable variables in javascript

Posted: Fri May 15, 2009 8:58 am
by steelcity_ballin
It seems to me that the guy who replied to my long-since-dead post, probably found us through a google search. It was his first post AFAIK. I personally have no problem have him discussing things in this thread but perhaps for clarity's sake in the future, titan is correct.