/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] alternative to a switch statement?

hello,
I am a fairly experienced programmer, but new to javascript. I have written the following script (simplified, of course) that works. the strings are all dynamically generated by php based on database queries, and the function is called by an onclick method which currently passes in a number. My biggest problem is that while there is currently only about 5 or 10 different strings maximum, that number could concievably rise to 100. My question is, Is there a different way to do it?
I tried calling

[CODE]writeNote(str3)[/CODE]

and then

[CODE]document.getElementById(“dynamic”).innerHTML = str3;[/CODE]

but I couldn’t make that work.
Thanks
Ray
p.s. I am aware that page load speed could be affected if I try to pass in 100 strings, especially if they are very big. I am watching it.

[CODE]
function writeNote(num)
{
str1=”<p>Text 1</p>”;
switch(num)
{
case ‘1’:
var outstr=str1
break;
default:
var outstr='<p>default text</p>’
}
document.getElementById(“dynamic”).innerHTML = outstr;
}
[/CODE]

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@cgishackFeb 18.2007 — what is the error you are getting, and are you sure str3 has a value ?

alert(str3) to see if you have a value in that var.
Copy linkTweet thisAlerts:
@pcx99Feb 18.2007 — Well the first and most obvious is to pass your strings as an array...

<i>
</i>str[1]="&lt;p&gt;Text 1&lt;/p&gt;";
function writeNote(num) {
if (!str[num]) {str[num]='default text';}
outstr=str[num];
}


If you can't do this, you can make a thousand javascript gurus cry and use eval.

<i>
</i>function writeNote(num)
{ str1="&lt;p&gt;Text 1&lt;/p&gt;";
eval("outstr=str"+num);
if (!outstr) {outstr='default text';}
document.getElementById("dynamic").innerHTML = outstr;
}
Copy linkTweet thisAlerts:
@felgallFeb 18.2007 — Or better yet don't use eval where it isn't needed

outstr=window['str'+num];
Copy linkTweet thisAlerts:
@rstillauthorFeb 18.2007 — Thank you pcx99,

your first solution may be exactly what I was looking for. I will check it out.

Ray

Dare I ask what's so horrible about eval?

(please educate me, not flame me.)
Copy linkTweet thisAlerts:
@pcx99Feb 18.2007 — Or better yet don't use eval where it isn't needed

outstr=window['str'+num];[/QUOTE]


How do you expect to make a thousand gurus cry if you don't use eval where it isn't needed? ?
Copy linkTweet thisAlerts:
@pcx99Feb 18.2007 — Thank you pcx99,

your first solution may be exactly what I was looking for. I will check it out.

Ray

Dare I ask what's so horrible about eval?

(please educate me, not flame me.)[/QUOTE]


Eval is the Javascript compiler, everything inside is basically javascript that's being created and executed on the fly which means two things...

1] It's slow.

2] It greatly increases the chance of introducing nearly untraceable bugs into the system.

That said, it's ok to use on JSON data as long as you implicitly trust the server and by implicitly I mean you'd feel comfortable giving that server your credit card info, social security number, diary, etc....

For most gurus eval can be summed up thusly (except for JSON see above):

http://javascript.crockford.com/code.html

eval is Evil

The eval function is the most misused feature of JavaScript. Avoid it.

eval has aliases. Do not use the Function constructor. Do not pass strings to setTimeout or setInterval.
Copy linkTweet thisAlerts:
@rstillauthorFeb 18.2007 — Thanks,

I feel educated but not any warmer ?

Ray
Copy linkTweet thisAlerts:
@konithomimoFeb 18.2007 — Use createElement() to make your P, and then store all of your strings in an array. Use a loop to search the array, and when you find the data you need simply insert it into the P via appendChild(document.createTextNode(yourval)); where yourval is replaced with the variable holding the correct dispaly value.
Copy linkTweet thisAlerts:
@rstillauthorFeb 18.2007 — The array solution works like a charm.

Two things I learned in implementing it.

1)The Array keyword is case sensitive :o

2)I can do away with the outstr variable completely by usingdocument.getElementById("dynamic").innerHTML = str[num];
(The topic on posting said to mark the thread resolved, but I can't figure out how. I'd be more than happy to if somebody could point out how)

Thanks very much

Ray
Copy linkTweet thisAlerts:
@KorFeb 18.2007 — Thread Tools > Mark Thread Resolved
×

Success!

Help @rstill 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.16,
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,
)...