473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Eval() Alternative?

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
3 6289
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select...
9
by: Mike | last post by:
After reading much on the evils of eval, I have a question using my own personal use of the function... We have a reports system that will generate reports based on a number of parameters...
7
by: steveneill | last post by:
Is this an alternative to eval(). Admittidly, I have done *very* little testing (ran it a few times in Firefox), but it seemed to work suprisingly well with simple expressions such as...
1
by: Newish | last post by:
Hi Is it really not recommended to us Eval in databound controls as follows: <td><asp:TextBox id="BookTitle" Runat="Server" Text='<%# Eval("BookTitle") %>' Font-Size="8pt"...
11
by: C.W.Holeman II | last post by:
I am looking for an example using the object argument to eval(). http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions:eval -- C.W.Holeman II | cwhii@Julian5Locals.com-5...
4
by: Jon Slaughter | last post by:
I'm using eval to excute some mixed php and html code but I cannot debug it. I am essentially using filegetcontents to load up a php/html file and then inserting it into another php/html file and...
7
by: Ciaran | last post by:
Can someone please give me a hand with this? $arr=array(2,4,5,6); $test='arr'; echo (eval("$".$test."")); I'm trying to get it to echo 2
13
by: My Pet Programmer | last post by:
The way I usually set up and work with the XMLHttpRequest to execute server side functions and get results is this: var url = "someurl?params=" + params; var conn = createRequest(); // gets an...
10
by: Gordon | last post by:
I have a script that creates new objects based on the value of a form field. Basically, the code looks like this. eval ('new ' + objType.value + '(val1, val2, val3'); objType is a select with...
4
by: spamme | last post by:
Hi, As you can see from the following code, I am trying to manipulate a large number of layers. I have read many times in the past how the "Eval" function should be used very sparingly as it can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.