473,609 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

eval


why won't the following work

for(var i=0;i<pics;i++) {
eval('img'+i) = new Image(wth,hgt)
eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
}

basically I am trying to create a numer of placeholders with thier
associated imgages!

thanks in advance
stuart
Jul 20 '05 #1
33 3749
> why won't the following work

for(var i=0;i<pics;i++) {
eval('img'+i) = new Image(wth,hgt)
eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
}


Where did you get this code?

Jul 20 '05 #2

Douglas Crockford <no****@laserli nk.net> wrote in message
news:55******** *************** ****@msgid.mega newsservers.com ...
why won't the following work

for(var i=0;i<pics;i++) {
eval('img'+i) = new Image(wth,hgt)
eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
}


Where did you get this code?


It is part of a bigger script that I am writting, the problem that I am
facing is that I cannot asign the src value to a constucted variable

I need to create a variable amount of placeholders and cache images such as

img1 = new Image(100,200)
img1.src = 'someURL'
img2 = new Image(100,200)
img2.src = 'someURL'
img3 = new Image(100,200)
img3.src = 'someURL'

I am trying to construct the placeholder through a loop as folows

for(var i=1;i<pics;i++) {
( img + i ) = new Image(100,200)
(img + i).src = 'someURL'
}

what I don't know is how to join img and i


Jul 20 '05 #3
Stuart wrote:
why won't the following work
Because it's evil[tm] eval(...). Besides, you are neither telling what you
are consider working nor what `pics' aso. are. Nevertheless, despite its
bad style, the below code does create Image objects and assigns a value to
their `src' property, so I consider it working.
for(var i=0;i<pics;i++) {
eval('img'+i) = new Image(wth,hgt)
eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
}

basically I am trying to create a numer of placeholders with thier
associated imgages!


Provided that `pics' stores the number of Image objects to create, that
`wth' and `hgt' are values for the width and height of *each* object and
that the image resources to be accessed are available via the URLs
http://www.mypics/X1.gif, where X is a number from 0 to pics - 1, use

var myImages = new Object();
for (var i = 0; i < pics; i++)
{
myImages['img' + i] = new Image(wth, hgt);
myImages['img' + i].src = 'http://www.mypics/' + i + '1.gif';
}

instead.
PointedEars

Jul 20 '05 #4
> > > why won't the following work

for(var i=0;i<pics;i++) {
eval('img'+i) = new Image(wth,hgt)
eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
}


Where did you get this code?


It is part of a bigger script that I am writting, the problem that I am
facing is that I cannot asign the src value to a constucted variable


Who told you to use eval()?

Jul 20 '05 #5
Thomas 'PointedEars' Lahn wrote:
Stuart wrote:
why won't the following work


Because it's evil[tm] eval(...). Besides, you are neither telling what you
are consider working nor what `pics' aso. are. Nevertheless, despite its
bad style, the below code does create Image objects and assigns a value to
their `src' property, so I consider it working.
for(var i=0;i<pics;i++) {
eval('img'+i) = new Image(wth,hgt)
eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
}


Hm, having tested it in Mozilla 1.5b and IE 6.0 SP-1, I'm getting a script
error because eval(...) isn't allowed as left-hand side expression (IE says
explicitely: "Cannot assign to a result of a function" (or similar,
translated it from German). Maybe I'm mixing up something here, but I
remember ECMAScript implementations where it was possible to use it there.
However, it's still bad style.
PointedEars

Jul 20 '05 #6

Douglas Crockford <no****@laserli nk.net> wrote in message
news:8a******** *************** ***@msgid.megan ewsservers.com. ..
> why won't the following work
>
> for(var i=0;i<pics;i++) {
> eval('img'+i) = new Image(wth,hgt)
> eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
> }

Where did you get this code?


It is part of a bigger script that I am writting, the problem that I am
facing is that I cannot asign the src value to a constucted variable


Who told you to use eval()?

I am trying anything....inc luding asking you!
Are you getting the jist of what I am trying to achieve?
Jul 20 '05 #7

Thomas 'PointedEars' Lahn <Po*********@we b.de> wrote in message
news:bo******** *****@ID-107532.news.uni-berlin.de...
Thomas 'PointedEars' Lahn wrote:
Stuart wrote:
why won't the following work
Because it's evil[tm] eval(...). Besides, you are neither telling what you are consider working nor what `pics' aso. are. Nevertheless, despite its
bad style, the below code does create Image objects and assigns a value to their `src' property, so I consider it working.
for(var i=0;i<pics;i++) {
eval('img'+i) = new Image(wth,hgt)
eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
}


Hm, having tested it in Mozilla 1.5b and IE 6.0 SP-1, I'm getting a script
error because eval(...) isn't allowed as left-hand side expression (IE

says explicitely: "Cannot assign to a result of a function" (or similar,
translated it from German). Maybe I'm mixing up something here, but I
remember ECMAScript implementations where it was possible to use it there.
However, it's still bad style.

Yes, I am getting these exact same errors,. However, I am pleased you
understood what I am trying to do, May be it is "bad style" but I am new to
javaScript, so may be you could point me in the way of "Good Style" and the
correct way of scripting.
Jul 20 '05 #8
Lee
Stuart said:


Douglas Crockford <no****@laserli nk.net> wrote in message
news:55******* *************** *****@msgid.meg anewsservers.co m...
> why won't the following work
>
> for(var i=0;i<pics;i++) {
> eval('img'+i) = new Image(wth,hgt)
> eval('img'+i+'. src') = 'http://www.mypics/'+i+'1.gif'
> }


Where did you get this code?


It is part of a bigger script that I am writting, the problem that I am
facing is that I cannot asign the src value to a constucted variable

I need to create a variable amount of placeholders and cache images such as

img1 = new Image(100,200)
img1.src = 'someURL'
img2 = new Image(100,200)
img2.src = 'someURL'
img3 = new Image(100,200)
img3.src = 'someURL'

I am trying to construct the placeholder through a loop as folows

for(var i=1;i<pics;i++) {
( img + i ) = new Image(100,200)
(img + i).src = 'someURL'
}

what I don't know is how to join img and i


Whenever you find yourself using eval(), you've probably
overlooked a simpler way to do something.

var img=new Array();
for(var i=0;i<pics;i++) {
img[i] = new Image(100,200);
img[i].src = 'someURL';
}

Jul 20 '05 #9
> > Who told you to use eval()?

I am trying anything....inc luding asking you!
Are you getting the jist of what I am trying to achieve?


It is almost always extremely wrong to use eval(). Who is teaching you to
program so badly?

Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4100
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed the script and special attention was thrown at the fact the script used eval alot. I don't use eval alot in my scripts - but I do use it - and since I always out to learn more / improve my javascript skills, I'm curious why something I thought...
11
1870
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before the use of eval() is required to execute it?" Given the following code, I'm able to evaluate/execute most expressions like: "a.b.c.d()"
0
3810
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval: 'System.Data.Common.DbDataRecord' does not contain a property with the name REPORTTO. Description: An unhandled exception occurred during the execution of the
18
3149
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My question is what happens to the dynamically created assembly when the method is done running? Does GC take care of it? Or is it stuck in RAM until the ASP.Net process is recycled? This code executes pretty frequently (maybe 4 times per transaction) and...
15
3651
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
3
1904
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do arrays each select is individually named withe MySelecti where i is an incremental from 1. I know all my variables are correct (i, OptionsCount) and my arrays of MyValues and MyDescription's exist for multiple enteries and if I bring out an...
4
2769
by: Jm lists | last post by:
Hello members, I want to know does the "eval" in python have the same features as in Perl (capture errors)? For example,in perl I can wrote: $re = eval { 1 / 0 }; Though 1/0 is a fatal error but since it's in "eval" block so the perl
6
5903
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
5
6626
by: wendallsan | last post by:
Hi all, I'm running into a situation where it seems that JS stops executing as soon as I call an eval in my script. I have an Ajax.Request call to a PHP page that builds a JS object and returns it as the responseText. I want to attach that object to the document (or anywhere else that would allow me to access it later), and to do that, I THINK I need to eval it because it's just a string otherwise. My problem is as soon as I execute a...
10
494
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 the different types of objects you can create as values. I really don't like using eval, and it's causing problems, like if I do something like the following:
0
8130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8573
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8406
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7002
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4021
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2531
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.