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

variable "preload"routine ?

P: n/a
Bob
I usually use some "pre-load" code in my pages to preload graphics
that will be swapped. But, I'm thinking that rather than the
long, repetitive, once, for each graphic hardcoded stuff like this:

var bb_off = new Image();
bb_off.src = "images/bb_off.jpg";

....that I could have an array where I just listed the names of the
graphics, a loop, and code in the loop that cycles through each
entry in the array to create and pre-load the "on" and "off"
graphics with code in the loop something like this:

for (...){
var temp = array[index] // contains text name for graphic like bb_off
var eval(temp) = new Image();
eval(temp).src = "images" +
eval(temp) + ".jpg"
}

But, it seems to choke on the first line - creation of an object
with an "eval" name. I tried some tricks with string objects but
there seemed to be problems with that. I suppose I could use a
document.write and just generate the code, but that seems like a lot
of extra work for the browser/system. I'd also like to avoid use
of anything too DOM new as I want backwards compatibility to
NN4 at least. To top it off, my JS is a little rusty. Is there some
way to create a variable/object like this with a varying name in JS ?

Thanks,
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Bob" <uc************@ultranet.com> wrote in message
news:oo********************************@4ax.com...
I usually use some "pre-load" code in my pages to preload graphics
that will be swapped. But, I'm thinking that rather than the
long, repetitive, once, for each graphic hardcoded stuff like this:

var bb_off = new Image();
bb_off.src = "images/bb_off.jpg";

...that I could have an array where I just listed the names of the
graphics, a loop, and code in the loop that cycles through each
entry in the array to create and pre-load the "on" and "off"
graphics with code in the loop something like this:

for (...){
var temp = array[index] // contains text name for graphic like bb_off
var eval(temp) = new Image();
eval(temp).src = "images" +
eval(temp) + ".jpg"
}

But, it seems to choke on the first line - creation of an object
with an "eval" name. I tried some tricks with string objects but
there seemed to be problems with that. I suppose I could use a
document.write and just generate the code, but that seems like a lot
of extra work for the browser/system. I'd also like to avoid use
of anything too DOM new as I want backwards compatibility to
NN4 at least. To top it off, my JS is a little rusty. Is there some
way to create a variable/object like this with a varying name in JS ?

Thanks,

<body onLoad="doPreload();">

function doPreload() {
var the_images = new Array('images/1.jpg','images/2.jpg',
'images/3.jpg');
preloadImages(the_images);
}
function preloadImages(the_images_array) {
for (var loop = 0; loop < the_images_array.length; loop++) {
var an_image = new Image();
an_image.src = the_images_array[loop];
}
}
Page 4 - Preloading Images - How's It Done?
http://hotwired.lycos.com/webmonkey/...tw=programming
Jul 20 '05 #2

P: n/a
> for (...){
var temp = array[index] // contains text name for graphic like bb_off
var eval(temp) = new Image();
eval(temp).src = "images" +
eval(temp) + ".jpg"
}


You can't treat eval(temp) as a variable in a declaration. Even if it
weren't a syntax error, it would equate to var "undefined = new Image();"

IF this is really how you want to do it, you have two option:
eval( "var " + temp + " = new Image();" );
eval( temp + ".src = 'images/" + temp + ".jpg';" );
OR
window[ temp ] = new Image( );
window[ temp ].src = 'images/' + temp + '.jpg';

A cleaner solution would be to stick each of these items as unnamed
variables in an array, along the lines of:
var images = [ 'bb_off', ... ];
for( var i in images )
{
images[ i ].obj = new Image( );
images[ i ].obj.src = 'images/' + images[ i ] + '.jpg';
}

Or, you could work it into a function:
var preload = function( )
{
for( var i = 0; i < arguments.length; i ++ )
{
this[ i ] = new Image( );
this[ i ].src = 'images/' + arguments[ i ] + '.jpg';
}
}
...
<body onload="preload( 'bb_off', ... );">

- Brian
Jul 20 '05 #3

P: n/a
JRS: In article <oo********************************@4ax.com>, seen in
news:comp.lang.javascript, Bob <uc************@ultranet.com> posted at
Fri, 19 Dec 2003 16:10:39 :-
I usually use some "pre-load" code in my pages to preload graphics
that will be swapped. But, I'm thinking that rather than the
long, repetitive, once, for each graphic hardcoded stuff like this:

var bb_off = new Image();
bb_off.src = "images/bb_off.jpg";
When code is repetitive, you can use a function for it. Then all you
need write for each image is var bb_off = ImLd("images/bb_off.jpg");
that is not necessarily quite as compact as using an array, but it is
easy to read.

function ImLd(S) { (T = new Image()).src = S ; return T }

var Sphere = ImLd("$orb.gif")
var bb_off = ImLd("images/bb_off.jpg")

seems to work for me.

Is there some
way to create a variable/object like this with a varying name in JS ?


The easiest may be to create an array and index it by the names - i.e.
not var Fred = ... ; var Jim = ... ; but var Ar = [] ; Ar["Fred"]
= ... ; Ar["Jim"] = ... ; .

This also creates a name sub-space for images, which seems clean.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.