473,881 Members | 1,545 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 3807
> 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
4116
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
1892
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
3820
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
3165
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
3679
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
3
1928
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
2798
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
5942
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
6642
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
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10716
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10400
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...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5780
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
5976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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
2
4194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.