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

speaking of eval() -- preload img question

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
3 1719
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: MALdito | last post by:
hi everybody let me say right from the start .. I´m not a coder ... "just" a designer! that said .. here is my question: I´m using dreamweaver´s built in preloader for a menu. it looks like...
4
by: Frustrated Newbe | last post by:
Hi, I would appreciate your opinion on the following issue. Suppose there is a windows (not web!) application that uses the data stored on an SQL server. There can be more than one concurrent...
2
by: Albert Spencil | last post by:
I have tried several preload scripts found here; plus, some of my own. The only thing that works is the unsophisticated loading of those tiny images. The download consist of 100+ images amounting...
1
by: Stacey | last post by:
Hi, I'm hoping for a bit of advise-- I have a (relatively, from the point-of-view of this dilettante) complex script that attempts to preload certain images in order to trigger one of a series of...
1
by: Peter Fastré | last post by:
Hello The javascript preload function I always used, is causing problems with the latest version of gecko-browsers. Never had any problems with it though. Maybe there's something wrong? ...
7
by: Andrew Poulos | last post by:
I've asked this question in a javascript group and I'm asking it here in case there's a HTML solution. I'm using HTML 4 Strict and I'm wondering if there's a way to preload a sound? Any solution...
3
by: phil2phil | last post by:
hi, had a question on preloading a dropdown. we have a textbox and dropdown on the page, user is suppose to enter in a zip into textbox, then the dropdown should load the state based on the zip. ...
10
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...
3
by: Revathi Balakrishnan | last post by:
Hi i have the used the below code to switch the button image on mouseover and mouseout. <html:button property="Button" value="Display" styleClass="displaybutton" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.