By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,934 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,934 IT Pros & Developers. It's quick & easy.

Eval() Alternative?

P: n/a
I said I wouldn't use "eval()" anymore but I need help to do it.

Below is some stripped-down code (4 lines; watch for word-wrap) extracted
from USGA.COM that preloads images:

main_nav_home_F1 = new Image(153,21); main_nav_home_F1.src =
"images/main_nav_home.gif";
main_nav_home_F2 = new Image(153,21); main_nav_home_F2.src =
"images/main_nav_home_F2.gif";
main_nav_about_F1 = new Image(153,21); main_nav_about_F1.src =
"images/main_nav_about.gif";
main_nav_about_F2 = new Image(153,21); main_nav_about_F2.src =
"images/main_nav_about_F2.gif";

Since there are two variations of 20 images, I thought a loop would be more
efficient.

The following works (I think) but it uses "eval()".

<script type="text/javascript">
if (document.images) {
var imgs = new Array();
imgs[0] = "home";
imgs[1] = "about";
// imgs[#] = etc.
var what = "";
for (var i=0; i<imgs.length; i++) {
what = "main_nav_" + imgs[i];
eval(what + "_F1 = new Image(153,21); " + what + "_F1.src =
\"images/" + what + ".gif\"");
eval(what + "_F2 = new Image(153,21); " + what + "_F2.src =
\"images/" + what + "_F2.gif\"");
}
}
</script>

I have 2 questions:

1) How would I eliminate the use of "eval()"?

2) Is there a better way to do this?

Thanks in advance.

P.S. I'm not with the USGA; I'm just considering the technique they used.
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
McKirahan wrote:
<snip>
<script type="text/javascript">
if (document.images) {
Why this test? Your function does not use the document.images
collection. The environment feature that it does use is the global -
Image - constructor so test to verify that function/object exists would
make more sense. Using - typeof - :-

if(typeof Image != 'undefined'){
...
}

- or a type-converting test:-

if(this.Image){ // the - this - keyword is a reference
// to the global object in code executing
// in the global context.
...
}

- or:-

if(window.Image){
...
}

(or via whatever other reference to the global object you have created
or is available)

Testing one object and then using another is "object inference" and is
not appropriate for Internet browser scripting (because the inferences
are not guaranteed to be valid).
var imgs = new Array();
imgs[0] = "home";
imgs[1] = "about";
// imgs[#] = etc.
var imgs = [ // Use an Array literal to pre-populate
// a created Array in one statement.
"home",
"about"
// etc. (paying attention to the commas
// in Array literals.)
];
var what = "";
for (var i=0; i<imgs.length; i++) {
what = "main_nav_" + imgs[i];
eval(what + "_F1 = new Image(153,21); " + what +
"_F1.src = \"images/" + what + ".gif\""); <snip>
1) How would I eliminate the use of "eval()"?
this[what+"_F1"] = new Image(153,21);
this[what+"_F1"].src = "images/"+ what+".gif";

- or:-

window[what+"_F1"] = new Image(153,21);
window[what+"_F1"].src = "images/"+ what+".gif";

- Or Creating a single global object:-

var exampleGlobalObject = {};

- and then assign the stored Image objects as named properties of that
object (reduces pollution of the global namespace):-

exampleGlobalObject[what+"_F1"] = new Image(153,21);
exampleGlobalObject[what+"_F1"].src = "images/"+ what+".gif";

And if you had read the resource linked to from quick answer 39 in the
FAQ you would not have had to ask this question.
2) Is there a better way to do this?


The posted code does not define the problem that it is intended to
solve, so there is no way of judging what would qualify as better.

Richard.
Jul 20 '05 #2

P: n/a
"McKirahan" <Ne**@McKirahan.com> wrote in message news:<PyJ%b.134150$uV3.654226@attbi_s51>...
hi McKirahan,
just answering your fist question:
1) How would I eliminate the use of "eval()"?

in javascript the dot operator "." is not the
one and only way of how object properties can
be addressed - there is always the alternative
with using the "square bracket syntax";

if there was an property prop01 belonging to
an object obj01 one could address it not only
like this obj01.prop01 but also
like that obj01["prop01"] with the second way
offering the chance for addressing objects and
object properties in case object names or property
names are available as string values only;

the given example is supposed to create gloabal
variables only by some known keywords and a known
naming schema;

for global variables the square bracket notation
can be used too, since those variables are nothing
else than properties of the scripting host - in web
browsers the host is represented by the window object;
(it should be familar to you cause it is used for
-just mentioning one example- addressing frames:
window.frames)

in order to create or to access a global variable
named "main_nav_home_F1" simply write:
window["main_nav_home_F1"]
using this for your example:

window["main_nav_home_F1"] = new Image();
window["main_nav_home_F1"].src = "whatEver.mime";

so long - peterS. - ps******@gmx.net
Jul 20 '05 #3

P: n/a
"peter seliger" <ps******@gmx.net> wrote in message
news:42**************************@posting.google.c om...
"McKirahan" <Ne**@McKirahan.com> wrote in message news:<PyJ%b.134150$uV3.654226@attbi_s51>...

hi McKirahan,
just answering your fist question:
1) How would I eliminate the use of "eval()"?

in javascript the dot operator "." is not the
one and only way of how object properties can
be addressed - there is always the alternative
with using the "square bracket syntax";


[snip]

Excellent clarification; thank you.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.