473,727 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable "preload"routin e ?

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
3 1743
"Bob" <uc************ @ultranet.com> wrote in message
news:oo******** *************** *********@4ax.c om...
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="doPrelo ad();">

function doPreload() {
var the_images = new Array('images/1.jpg','images/2.jpg',
'images/3.jpg');
preloadImages(t he_images);
}
function preloadImages(t he_images_array ) {
for (var loop = 0; loop < the_images_arra y.length; loop++) {
var an_image = new Image();
an_image.src = the_images_arra y[loop];
}
}
Page 4 - Preloading Images - How's It Done?
http://hotwired.lycos.com/webmonkey/...tw=programming
Jul 20 '05 #2
> 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.lengt h; i ++ )
{
this[ i ] = new Image( );
this[ i ].src = 'images/' + arguments[ i ] + '.jpg';
}
}
...
<body onload="preload ( 'bb_off', ... );">

- Brian
Jul 20 '05 #3
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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4559
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable name="Tim">ZXCVBNM</Variable> <Function id="1"> <Parameter type="String">Bob</Parameter> <Parameter type="String">Steve</Parameter>
4
3246
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable name="Banner_error_2"> errormessage 2 for banner </xsl:variable>
134
7884
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
10
2252
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal multiplyer as integer, ByVal variable as ___?) variable = 5 * multiplyer end function What I would like this function to do is take the name of the incoming variable and assign a calculated value to it. Any help would be greatly appreciated, TIA!!
23
19199
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
3963
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable name="fileName">../../data/models/<xsl:value-of select="."/>.xml</xsl:variable> I want to use this variable as an argument of the document function: <xsl:apply-templates mode="coordinates" select="document( REFERENCE
1
25690
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
2
3679
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
37
5469
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods are the one which runs statically with the defining class. Hence, my understanding is that static variables must be bound to the class defining the variables and shared by children of parent class where the variable is defined. But, please have a...
112
5451
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
8891
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9406
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9260
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9185
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8103
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6703
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4786
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.