473,326 Members | 2,127 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,326 software developers and data experts.

More on Eval

Hello-

I have a javascript problem which seems that it ought to be soluble without eval, but I can only make it work with eval.

With the script below the call:
"ImageChange('myImageName', newImage);" works fine, but:
"channelChange('myImageName');" does not. The string anewsrc is being treated as a string, and not as an object even though I am asking for it's "src" property.

--------------------
function ImageChange(imagename, newsrc)
{
document.images[imagename].src=newsrc["src"];
}

function channelChange(newchan) {
var anewsrc;
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].stackname) {
anewsrc=(document.images[i].stackname + "_" + newchan);
ImageChange(document.images[i].stackname, anewsrc);
}
}
}
-------------------------


However switching to :
--------------------
function ImageChange(imagename, newsrc)
{
document.images[imagename].src=newsrc["src"];
}

function channelChange(newchan) {
var anewsrc;
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].stackname) {
anewsrc=(document.images[i].stackname + "_" + newchan);
ImageChange(document.images[i].stackname, eval(anewsrc)); //<-!-
}
}
}
-------------------------

allows it to work with both calls.

Is there a better alternative here?

Thanks,
Leo
Jan 3 '07 #1
6 1361
b1randon
171 Expert 100+
Hello-

I have a javascript problem which seems that it ought to be soluble without eval, but I can only make it work with eval.

With the script below the call:
"ImageChange('myImageName', newImage);" works fine, but:
"channelChange('myImageName');" does not. The string anewsrc is being treated as a string, and not as an object even though I am asking for it's "src" property.

--------------------
function ImageChange(imagename, newsrc)
{
document.images[imagename].src=newsrc["src"];
}

function channelChange(newchan) {
var anewsrc;
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].stackname) {
anewsrc=(document.images[i].stackname + "_" + newchan);
ImageChange(document.images[i].stackname, anewsrc);
}
}
}
-------------------------


However switching to :
--------------------
function ImageChange(imagename, newsrc)
{
document.images[imagename].src=newsrc["src"];
}

function channelChange(newchan) {
var anewsrc;
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].stackname) {
anewsrc=(document.images[i].stackname + "_" + newchan);
ImageChange(document.images[i].stackname, eval(anewsrc)); //<-!-
}
}
}
-------------------------

allows it to work with both calls.

Is there a better alternative here?

Thanks,
Leo
Leo, I'm not sure at all what this stackname stuff you're using in your code is, so I pulled it out. To fix the src problem you'll need to assign an Image objects src member to the images src. If that sounds confusing just check out my example and it should help.

[HTML]
<html>
<head><script type="text/javascript" src="script.js"></script></head>
<body>
<image name="img1" src="hab.jpg" />
<input type="button" value="change" onclick="channelChange('cal.GIF');" />
</body>
</html>
[/HTML]
&
Expand|Select|Wrap|Line Numbers
  1. function ImageChange(imagename, newsrc)
  2. {
  3.     document.images[imagename].src=newsrc.src;
  4. }
  5.  
  6. function channelChange(newchan) {
  7.     var tempImg = new Image();
  8.     tempImg.src = newchan;
  9.     for (var i = 0; i < document.images.length; i++) {
  10.         ImageChange(document.images[i].name, tempImg);
  11.     }
  12. }
  13.  
Jan 3 '07 #2
I have several groups (or stacks) of images, and I want to change them all from the first image in the stack to the second with one click. The channelChange function is designed to find each image that has a 'stack' defined and then pass to the imageChange function the name of the stack concatenated with the name of the layer to be shown. I think it is the concatenation of the string used in anewsrc that is causing the problem.

anewsrc=(document.images[i].stackname + "_" + anewchan);

The stackname attribute is just a name prefix allowing me to know which of the images I should replace the current one with.

For example I might have images named A_1.png, A_2.png, B_1.png and B_2.png. If A_1.png and B_1.png are showing (A and B would be the stacknames), I want channelChange to switch them both to show A_2.png and B_2.png. Thus I need to build up the new source from the current stackname and the requested channel, for each stack on the page.

Does that make the problem clearer?

Thanks for your help,
Leo



Leo, I'm not sure at all what this stackname stuff you're using in your code is, so I pulled it out. To fix the src problem you'll need to assign an Image objects src member to the images src. If that sounds confusing just check out my example and it should help.

[HTML]
<html>
<head><script type="text/javascript" src="script.js"></script></head>
<body>
<image name="img1" src="hab.jpg" />
<input type="button" value="change" onclick="channelChange('cal.GIF');" />
</body>
</html>
[/HTML]
&
Expand|Select|Wrap|Line Numbers
  1. function ImageChange(imagename, newsrc)
  2. {
  3.     document.images[imagename].src=newsrc.src;
  4. }
  5.  
  6. function channelChange(newchan) {
  7.     var tempImg = new Image();
  8.     tempImg.src = newchan;
  9.     for (var i = 0; i < document.images.length; i++) {
  10.         ImageChange(document.images[i].name, tempImg);
  11.     }
  12. }
  13.  
Jan 3 '07 #3
acoder
16,027 Expert Mod 8TB
Hello-

I have a javascript problem which seems that it ought to be soluble without eval, but I can only make it work with eval.

With the script below the call:
"ImageChange('myImageName', newImage);" works fine, but:
"channelChange('myImageName');" does not. The string anewsrc is being treated as a string, and not as an object even though I am asking for it's "src" property.

--------------------
function ImageChange(imagename, newsrc)
{
document.images[imagename].src=newsrc["src"];
}

function channelChange(newchan) {
var anewsrc;
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].stackname) {
anewsrc=(document.images[i].stackname + "_" + newchan);
ImageChange(document.images[i].stackname, anewsrc);
}
}
}
-------------------------


However switching to :
--------------------
function ImageChange(imagename, newsrc)
{
document.images[imagename].src=newsrc["src"];
}

function channelChange(newchan) {
var anewsrc;
for (var i = 0; i < document.images.length; i++) {
if (document.images[i].stackname) {
anewsrc=(document.images[i].stackname + "_" + newchan);
ImageChange(document.images[i].stackname, eval(anewsrc)); //<-!-
}
}
}
-------------------------

allows it to work with both calls.

Is there a better alternative here?

Thanks,
Leo
Instead of eval use arrays:
Expand|Select|Wrap|Line Numbers
  1. anewsrc=document.images[i][stackname + "_" + newchan];
Does that work?
Jan 3 '07 #4
Interesting suggestion, but no.

There is no such element as document.images[i][stackname + "_" + newchan];

are you suggesting that I should store the images in an array?

Right now the images are in the document thusly, created at runtime by the application that builds this page:
A1000001_00= new Image(); A1000001_00.src="A1000001_00.png"; A1000001_00.rel = "FSIA1000001_00.png";
A1000001_01= new Image(); A1000001_01.src="A1000001_01.png"; A1000001_01.rel = "FSIA1000001_01.png";
A1000001_02= new Image(); A1000001_02.src="A1000001_02.png"; A1000001_02.rel = "FSIA1000001_02.png";A1000002_00= new Image(); A1000002_00.src="A1000002_00.png"; A1000002_00.rel = "FSIA1000002_00.png";
A1000002_01= new Image(); A1000002_01.src="A1000002_01.png"; A1000002_01.rel = "FSIA1000002_01.png";
A1000002_02= new Image(); A1000002_02.src="A1000002_02.png"; A1000002_02.rel = "FSIA1000002_02.png";
etc...


Instead of eval use arrays:
Expand|Select|Wrap|Line Numbers
  1. anewsrc=document.images[i][stackname + "_" + newchan];
Does that work?
Jan 3 '07 #5
b1randon
171 Expert 100+
Interesting suggestion, but no.

There is no such element as document.images[i][stackname + "_" + newchan];

are you suggesting that I should store the images in an array?

Right now the images are in the document thusly, created at runtime by the application that builds this page:
A1000001_00= new Image(); A1000001_00.src="A1000001_00.png"; A1000001_00.rel = "FSIA1000001_00.png";
A1000001_01= new Image(); A1000001_01.src="A1000001_01.png"; A1000001_01.rel = "FSIA1000001_01.png";
A1000001_02= new Image(); A1000001_02.src="A1000001_02.png"; A1000001_02.rel = "FSIA1000001_02.png";A1000002_00= new Image(); A1000002_00.src="A1000002_00.png"; A1000002_00.rel = "FSIA1000002_00.png";
A1000002_01= new Image(); A1000002_01.src="A1000002_01.png"; A1000002_01.rel = "FSIA1000002_01.png";
A1000002_02= new Image(); A1000002_02.src="A1000002_02.png"; A1000002_02.rel = "FSIA1000002_02.png";
etc...
For sure. That will make your code 1000x easier. Just load all of your images into an array, then loop over changing the source as my first code block demonstrated. This is a much more controlled approach.
Jan 4 '07 #6
Ok, that sounds good. Can you point me in the right direction for loading the images into an array. None of them are to be displayed until the event requesting them.

Thanks,
Leo

For sure. That will make your code 1000x easier. Just load all of your images into an array, then loop over changing the source as my first code block demonstrated. This is a much more controlled approach.
Jan 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Wayne Wengert | last post by:
I am trying to use a repeater control on a .NEW Web Form. I modelled my code after the example in the on-line help (see code below) but VSNET complains that: "Within a server element, the element...
4
by: trond | last post by:
Hello all, Before I start I'd like to point out that I am a complete novice when it comes to asp.net - My background is in network and operating systems, and although I have been doing a bit of...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
3
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...
4
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...
2
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i'm in a template column of a gridview. i have a hyperlink in the Item Template and i'm trying to do a custom binding expression to it. So far i have the following: Eval("FILE_ID",...
5
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...
2
by: hp_1981 | last post by:
Hi, Which one has more performance and speed? using dataset and databinding asp.net controls or building string by SqlDataReader ? the following is my two methods: 1. Using SqlDataReader :...
0
by: Lie Ryan | last post by:
On Tue, 30 Sep 2008 16:04:34 -0500, William Purcell wrote: when you pass mydict, it is used as the global variables in the eval, right? Then, you passed a code to eval('...', mydict), sometimes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.