/    Sign up×
Community /Pin to ProfileBookmark

variables and concatination

I know I am missing something simple but I have some variables that equal a path and file name:

overview_path = “data/overview/”;
console_path = “data/console/”;

overview_page_list = new Array();
console_page_list = new Array();

console_page_list[0] = “a1-purpose.html”;
console_page_list[1] = “a2-components.html”;

I have a function trying to dynamically put them together.

function getGroupPages(whichGroup) {
if (whichGroup!=null)
{
groupInUse = whichGroup;
}
var path = groupInUse + “_path”;
var list = groupInUse + “_
page_list”;

top.Main.location = path + list+”[0]”;
}

If I call the function with the following parameter

getGroupPages(“console”);

I want the file data/overview/a1-purpose.html to goto the Main frame. But when I use a debugging alert function to view path+list+”[0]” I get console_pathconsole_page_list[0] instead of what of the inners of data/overview/a1-purpose.html Can anyone help?

to post a comment
JavaScript

20 Comments(s)

Copy linkTweet thisAlerts:
@BillyRayAug 23.2004 — This should solve your immediate problem. Change the last line of your "getGroupPages" function to this:

top.Main.location = window[path] + window[list][0];
However, I strongly suggest you look at another way of achieving the same result by restructuring the way you are storing and retrieving the data.

Hope this helps,

Dan
Copy linkTweet thisAlerts:
@WestPacAug 23.2004 — Try this:

<HTML>

<Head>

<Script Language=JavaScript>

var overview_path = "data/overview/";
var console_path = "data/console/";

overview_page_list = new Array();

console_page_list = new Array();
console_page_list[0] = "a1-purpose.html";
console_page_list[1] = "a2-components.html";


function getGroupPages(whichGroup,n) {

groupInUse = whichGroup;

path = eval(groupInUse+"_path");
list = eval(groupInUse+"_page_list"+"[n]");
full = path+list;

alert(full);

//top.Main.location = full;
}


</Script>

</Head>

<Body>

<br><br><br>

<center>

<input type=button value='Concat Console Group' onClick="getGroupPages('console',0)">

</Body>

</HTML>
Copy linkTweet thisAlerts:
@Mr_JAug 23.2004 — You don't want to use eval() if at all possible
Copy linkTweet thisAlerts:
@WestPacAug 23.2004 — Killiank:

If upon meeting someone for the first time, he introduced himself as Supreme Master of the Web, wouldn't you just giggle? If not, then there are two fools, you, and the person who fancies himself to be supreme.

There are several of them running around loose here. You can easily spot them, because of such goofy monikers, or juvenile 'signatures' quoting some asinine 'professor' or repeating some arcane prose.

More often then not, they pose a question to your question, or they attempt to persuade you from doing what you want to do, in the manner that you want to do it. Curiously though, on the rare occasions that they actually offer code, it's a mere snippet, containing such garbage like "foo." Never do they offer a complete working example which directs itself to solving your problem.

I regard them the same way I would a toddler stomping his feet, "look at me!, look at me!, look at me!"

Right, that's helpful. Tell me to find another way. Great. Tell me to not use something that works. Gee, thanks. Go ahead. Stomp your feet.
Copy linkTweet thisAlerts:
@steelersfan88Aug 23.2004 — [i]Originally posted by WestPac [/i]

[B] If upon meeting someone for the first time, he introduced himself as Supreme Master of the Web, wouldn't you just giggle? If not, then there are two fools, you, and the person who fancies himself to be supreme.

There are several of them running around loose here. You can easily spot them, because of such goofy monikers, or juvenile 'signatures' quoting some asinine 'professor' or repeating some arcane prose. [/b]
[/quote]
This is the rudest thing I've ever heard. The peple you are referring to are the most respecte dof members, who vounteer here to help make the Web more accessible.[i]Originally posted by WestPac [/i]

[B] More often then not, they pose a question to your question, or they attempt to persuade you from doing what you want to do, in the manner that you want to do it. Curiously though, on the rare occasions that they actually offer code, it's a mere snippet, containing such garbage like "foo." Never do they offer a complete working example which directs itself to solving your problem. [/b][/quote]
This is completely incorrect. Members here want to help you learn (see below)[i]Originally posted by WestPac [/i]

[B] I regard them the same way I would a toddler stomping his feet, "look at me!, look at me!, look at me!"

Right, that's helpful. Tell me to find another way. Great. Tell me to not use something that works. Gee, thanks. Go ahead. Stomp your feet. [/B]
[/QUOTE]
Please calm down. The supreme master of the web is an auto-title for his post count, provided he doesn't change it. And moreover, he is absolutely correct, and you are absolutely wrong.

The members here that use things like "foo" are the people who are intelligent enough to give you examples, so you actually do work, which helps you build knowledge, rather than what you want, which helps you build your copy/paste skills.

Before you begin to whine again, I will provide you the correct way to remove eval, using the window object:path = window[groupInUse+"_path"];
list = window[groupInUse+"_page_list"][n];
That works because the following is true:var str = "My String";
alert(str == window['str']); // alerts true
You really should work on the way you present yourself. Be thankful you have received the help you have gotten.

Dr. Script
Copy linkTweet thisAlerts:
@JPnycAug 23.2004 — [i]Originally posted by WestPac [/i]

[B]Killiank:

If upon meeting someone for the first time, he introduced himself as Supreme Master of the Web, wouldn't you just giggle? If not, then there are two fools, you, and the person who fancies himself to be supreme.

There are several of them running around loose here. You can easily spot them, because of such goofy monikers, or juvenile 'signatures' quoting some asinine 'professor' or repeating some arcane prose.

More often then not, they pose a question to your question, or they attempt to persuade you from doing what you want to do, in the manner that you want to do it. Curiously though, on the rare occasions that they actually offer code, it's a mere snippet, containing such garbage like "foo." Never do they offer a complete working example which directs itself to solving your problem.

I regard them the same way I would a toddler stomping his feet, "look at me!, look at me!, look at me!"

Right, that's helpful. Tell me to find another way. Great. Tell me to not use something that works. Gee, thanks. Go ahead. Stomp your feet. [/B]
[/QUOTE]


Perhaps if you waited until you amassed more than 9 posts, and were somewhat familiar with the board's function, before lashing out in ignorance at fellow members, you'd be aware that all designations such as webmaster, supreme master of the web, etc. are BOARD attached designations associated with post totals. They have NOTHING to do with member choices. They are NOT member selected. Now you may attempt to extricate your foot from your mouth.
Copy linkTweet thisAlerts:
@steelersfan88Aug 23.2004 — [i]Originally posted by DUNSEL [/i]

[B]Perhaps if you waited until you amassed more than 9 posts, and were somwhat familiar with the board's function, before lashing out in ignorance at fellow members, you'd be aware that all designations such as webmaster, supreme master of the web, etc. are BOARD attached designations associated with post totals. They have NOTHING to do with member choices. They are NOT member selected. Now you may attempt to extricate your foot from your mouth. [/B][/QUOTE]
What do you think you are doing DUNSEL. You are not acting at all "supreme". hehe, absolutely true what you said.
Copy linkTweet thisAlerts:
@JPnycAug 23.2004 — Sorry I just never saw anyone respond with such unprovoked blatant belligerence before, on here at least.
Copy linkTweet thisAlerts:
@steelersfan88Aug 23.2004 — [i]Originally posted by DUNSEL [/i]

[B]Sorry I just never saw anyone respond with such unprovoked blatant belligerence before, on here at least. [/B][/QUOTE]
I've seen it a bunch ... but normally in the fashion of "I don't want you modifying my code you <insert bad language here>".
Copy linkTweet thisAlerts:
@WestPacAug 23.2004 — Killiank:

I avoided attempting to provide you with a list of the screen names of the toddlers, but, predictably, they have volunteered to be included. It's not a complete list. But, useful, just the same.
Copy linkTweet thisAlerts:
@steelersfan88Aug 23.2004 — haha ... There are no toddlers here except for yourself, and everyone here, and every other volunteer here will agree to that. We've done everything to make your code better, and if you find a problem in us making your code better, you'll experience quite a few problems ... and we'd recommend you leave ...
Copy linkTweet thisAlerts:
@Mr_JAug 23.2004 — I wasn't going to reply to this but I feel that I must at least thank [B]Steelersfan88[/B] and [B]Dunsel[/B] for jumping to my defense.

I cannot really see where the problem is with [B]Westpac[/B], I offered a simple bit of sound advice.

[B]BillyRay[/B] had already given the ideal solution and in my opinion to use eval() was a step in the wrong direction.

I did not say [B]do not use eval()[/B] what I actually said was

You don't want to use eval() if at all possible[/QUOTE]

to me there is a very big difference in the wording.

Rather than going into an extremely long explanation of the right and wrongs of eval() I thought a simply one liner would be suffice and if [B]killiank[/B] wanted more information he would either ask here or on google.

To quote a collegue "liorean" over at codingforums.com who has done more research than I on eval()


I've done some benchmarking on using eval('document.images.'+sName) versus document.images[sName]. I've found eval to take about 12 times the time the associative array takes in op7 (op6 had a radically different scripting engine where everything took much more time, but the comparison made eval just 1.5 times slower), 2.5 times in moz, 5 times in ie, 7.5 in nn4.

Eval also is a really hungry memory eater while executing.

Depending on what you use eval for, you can often replace it with more efficient code.

Associative arrays are the most usual replacement.)
[/QUOTE]


And I don't mind being the "Supreme Master of the Web" honest?

Ta!
Copy linkTweet thisAlerts:
@WestPacAug 23.2004 — Only a toddler takes something that works, and claims "to make it better." That's not arrogant, or egotistical, that's childish. Go ahead, find all my posts, now and into the future, and attack, attack attack. Adults easily recoginize a juvenile.
Copy linkTweet thisAlerts:
@steelersfan88Aug 23.2004 — [i]Originally posted by Mr J [/i]

[B]I wasn't going to reply to this but I feel that I must at least thank [B]Steelersfan88[/B] and [B]Dunsel[/B] for jumping to my defense.[/B][/QUOTE]
That is what toddlers do, and we three know like the best of 'em.

I was planning to do some research on my own to prove the point ... glad that you got it before I decided to do so ?
Copy linkTweet thisAlerts:
@JPnycAug 23.2004 — I suggest, in order to prevent this from degenerating any further, to cease replying, report the post(s) to a mod if you feel it/they warrant(s) it, and let them deal with it, or not. Just my 2 cents. :-)
Copy linkTweet thisAlerts:
@killiankauthorAug 24.2004 — BillyRay's solution did work very well for me...Thanks. I was going to ask what was so undesirable about using eval()...I see you already posted it amoungst the little spat we had here. Thank you everyone for your help! This is a great forum; I always get good help and usually in a way that I can learn how it works.

Kodie
Copy linkTweet thisAlerts:
@BillyRayAug 24.2004 — Good to see the "window[]" solution worked for you. I wrote this test harness a few days ago to illustrate to someone on another forum how eval can slow things down.

All it does is query the value of a form element 100,000 times. Without eval, it takes 3.7 seconds. With eval, it takes 12 seconds. A huge difference, I'm sure you'll agree:

&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;
&lt;!--
function testTimes() {
var numIterations = 100000;
var outputHTML = '';

<i> </i> var startTime = new Date().getTime();
<i> </i> for (var loop=0; loop&lt;numIterations; loop++) var testValue = document.forms[0].elements['test1'].value;
<i> </i> var endTime = new Date().getTime();
<i> </i> outputHTML += 'Time taken without eval for ' + numIterations + ' iterations: ' + ((endTime-startTime)/1000) + ' seconds';
<i> </i> outputHTML += '&lt;br /&gt;';

<i> </i> var startTime = new Date().getTime();
<i> </i> for (var loop=0; loop&lt;numIterations; loop++) var testValue = eval('document.forms[0].elements[\'test' + 1 + '\'].value');
<i> </i> var endTime = new Date().getTime();
<i> </i> outputHTML += 'Time taken with eval for ' + numIterations + ' iterations: ' + ((endTime-startTime)/1000) + ' seconds';

<i> </i> document.getElementsByTagName('body')[0].innerHTML = outputHTML;
<i> </i> }
<i> </i>//--&gt;
<i> </i>&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="testTimes();"&gt;
&lt;form&gt;
&lt;input type="hidden" name="test1" value="Test" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Dan
Copy linkTweet thisAlerts:
@spetzle2002Aug 24.2004 — Everyones so helpful in this site, please stop arguing about some codes and get back to our normal tasks...HELPING EACH OTHER TO BUILD A BETTER WORLD!!!Of WEBMASTERS!
Copy linkTweet thisAlerts:
@jwkolkerJun 27.2005 — I have been trying to build this for over an hour and can't quite get it to work.

I want to have 3 selection fields in a form

State

County

OfficeType

Let's say we select the following values for each

PA

Bucks

Dentist


I want to click on a submit button that will send me to

/directory/PABucksDentist.html

Basically this is a 3 variabled jump menu - which concatinates the string values of each selection field into a URL

any ideas would be greatly appreciated.

Regards,

John Kolker
Copy linkTweet thisAlerts:
@Mr_JJun 28.2005 — Here's a really rough example, there may be better ways

[code=php]<script type="text/javascript">
<!--
directory="folder/"

function test(){
var v1=document.f1.s1.options[document.f1.s1.selectedIndex].value;
var v2=document.f1.s2.options[document.f1.s2.selectedIndex].value;
var v3=document.f1.s3.options[document.f1.s3.selectedIndex].value;

str=directory+v1+v2+v3+".html"

alert(str)

}

//-->
</script>


<form name="f1" onsubmit="return test()">

<select name="s1">
<option value="PA">PA
<option value="Two">Two
<option value="Fee">Fee
</select>

<select name="s2">
<option value="Bucks">Bucks
<option value="Pints">Pints
<option value="Fie">Fie
</select>

<select name="s3">
<option value="Dentists">Dentists
<option value="Please">Please
<option value="Foe">Foe
</select>

<input type="submit" value="Test">

</form>[/code]
×

Success!

Help @killiank spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...