speaking of eval() -- preload img question

M

Matthew Crouch

okay, i've got a server-side (php) script creating a bunch of JS image
vars in a loop ..

{{foreach from=$button_names_array key=key item=button_name}}
var {{$button_name}}_up = new Image();
{{$button_name}}_up.src =
"{{$LAYOUT_IMAGE_DIR}}/gnav/{{$button_name}}_off.gif" ;
var {{$button_name}}_down = new Image() ;
{{$button_name}}_down.src =
"{{$LAYOUT_IMAGE_DIR}}/gnav/{{$button_name}}_on.gif" ;

(those are smarty tags -- it's not important)

the doPreload() I inherited .. have no idea where it came from

function doPreload(imgObj,imgSrc) {
if (document.images) {
eval(imgObj + ' = new Image()');
eval(imgObj + '.src = "' + imgSrc + '"');
}
}
But I (finally) have a javascript console going, and it warns
"assignment to undeclared variable gnav_ho (the name of the image) ... "
Then, obviously, similar warning for each one in the loop.

For kicks i changed to
eval(var imgObj + ' = new Image()');
which made the warning go away, but the image doesn't show up. I
personally don't care -- I'll use the working/warning version ... just
thought this might be a good potential learning experience, as
undecalred vars are giving me trouble routinely....

Thanks to all, you're slowly convincing me that this language is worth
learning.
 
L

Lee

Matthew Crouch said:
okay, i've got a server-side (php) script creating a bunch of JS image
vars in a loop ..

{{foreach from=$button_names_array key=key item=button_name}}
var {{$button_name}}_up = new Image();
{{$button_name}}_up.src =
"{{$LAYOUT_IMAGE_DIR}}/gnav/{{$button_name}}_off.gif" ;
var {{$button_name}}_down = new Image() ;
{{$button_name}}_down.src =
"{{$LAYOUT_IMAGE_DIR}}/gnav/{{$button_name}}_on.gif" ;

(those are smarty tags -- it's not important)

the doPreload() I inherited .. have no idea where it came from

function doPreload(imgObj,imgSrc) {
if (document.images) {
eval(imgObj + ' = new Image()');
eval(imgObj + '.src = "' + imgSrc + '"');
}
}
But I (finally) have a javascript console going, and it warns
"assignment to undeclared variable gnav_ho (the name of the image) ... "
Then, obviously, similar warning for each one in the loop.

For kicks i changed to
eval(var imgObj + ' = new Image()');
which made the warning go away, but the image doesn't show up. I
personally don't care -- I'll use the working/warning version ... just
thought this might be a good potential learning experience, as
undecalred vars are giving me trouble routinely....

What you should eventually do is eliminate the preload function
and generate the same sort of assignments for these images that
you're using for your button names. Both are pre-loading the
images, but it's much safer and much more efficient to generate
the code on the server than to generate code on the client.

The images don't appear when you declare them as "var" inside
the eval() context because that makes them local to the scope
of the evaluated expression, which is not available to the
rest of the page.
 
E

Evertjan.

Matthew Crouch wrote on 07 okt 2005 in comp.lang.javascript:
function doPreload(imgObj,imgSrc) {
if (document.images) {
eval(imgObj + ' = new Image()');
eval(imgObj + '.src = "' + imgSrc + '"');
}
}

Eval is evil !!!!!!!
And there is no earthly way why you would need eval() here.

function doPreload(imgObj,imgSrc) {
if (document.images) {
imgObj = new Image();
imgObj.src = imgSrc;
}
}

Remember:
this way you heve to declare the object name outside the function.
 
R

Randy Webb

Evertjan. said the following on 10/7/2005 1:45 PM:
Matthew Crouch wrote on 07 okt 2005 in comp.lang.javascript:




Eval is evil !!!!!!!
And there is no earthly way why you would need eval() here.

function doPreload(imgObj,imgSrc) {
if (document.images) {

if (window.Image)

window.Image is an image holder, document.images is the collection
referring to all images in the page. Subtle but different.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,199
Latest member
AnyaFlynn6

Latest Threads

Top