By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,814 Members | 2,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,814 IT Pros & Developers. It's quick & easy.

speaking of eval() -- preload img question

P: n/a
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.
Oct 7 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Oct 7 '05 #2

P: n/a
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.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 7 '05 #3

P: n/a
Evertjan. said the following on 10/7/2005 1:45 PM:
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) {


if (window.Image)

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

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 7 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.