Variable variables in javascript

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Variable variables in javascript

Postposted on Mon Jan 12, 2009 8:18 am

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:

Code: Select all
<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".
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11924
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Variable variables in javascript

Postposted on Mon Jan 12, 2009 8:43 am

I fixed it myself.

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


Gah... so simple... :oops:
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11924
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Variable variables in javascript

Postposted on Thu May 14, 2009 5:15 am

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
Last edited by rimmazz on Tue May 19, 2009 1:34 am, edited 1 time in total.
rimmazz
Gerbil In Training
 
Posts: 1
Joined: Wed May 13, 2009 6:12 am

Re: Variable variables in javascript

Postposted on Thu May 14, 2009 5:42 am

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.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11924
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Variable variables in javascript

Postposted on Thu May 14, 2009 10:28 am

I know this is way late, but
steelcity_ballin wrote:Gah... so simple... :oops:
why aren't you using an array for this?
UberGerbil
Gerbil Khan
 
Posts: 9999
Joined: Thu Jun 19, 2003 3:11 pm

Re: Variable variables in javascript

Postposted on Thu May 14, 2009 1:43 pm

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.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11924
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA

Re: Variable variables in javascript

Postposted on Thu May 14, 2009 8:26 pm

I think there should be a thread split here.

Cookies are pretty well covered with an appropriate Google search.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains

Re: Variable variables in javascript

Postposted on Fri May 15, 2009 8:58 am

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.
Corsair 600T | ASUS P8P67 PRO | Intel 2500k @ 4.4Ghz | EVGA 560 TI | G.SKILL Ripjaws Series 8GB | Corsair HX650 650W
steelcity_ballin
Gerbilus Supremus
Silver subscriber
 
 
Posts: 11924
Joined: Mon May 26, 2003 5:55 am
Location: Pittsburgh PA


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest