473,509 Members | 11,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Defining pictures in an array

I wish to access several pictures on my page by defining them as an
array. This way I can either loop through them or access them by array

index. I am a beginner at Javascript so please keep your answers as
simple as possible.

What I am really doing is writing my own picture gallery and slide
show. The reason I am doing this is so that I can have my web page
look exactly the way I want. In addition, it is an excellent way to
learn Javascript.

Jun 15 '06 #1
2 1327
Hi,

Helpful person wrote:
I wish to access several pictures on my page by defining them as an
array. This way I can either loop through them or access them by array

index. I am a beginner at Javascript so please keep your answers as
simple as possible.

What I am really doing is writing my own picture gallery and slide
show. The reason I am doing this is so that I can have my web page
look exactly the way I want. In addition, it is an excellent way to
learn Javascript.


You can definitely store images in an array. Note that when you set the
src attribute of an Image object, this forces the image to be loaded
from the server to the client in the background. This may be what you
want, but maybe not, so it's up to you to decide. If you want to load
the images only before they'll be displayed, you should rather store the
image's location (a URL as string) in the array, and create the Image
object only later.

Here is a starter. The code can be much more refined, but this will give
you a starte.

function onLoad()
{
// This function will be called every time that an image is
// loaded successfully. You can use it to count the number
// of images, for example. Dure to the nature of events in
// JavaScript, you cannot directly pass parameters to this
// kind of functions. There is a way to do it anyway,
// using what is called "closure", but I suggest that you
// deal with that later, as it is quite a sophisticated concept.
}

function onError()
{
// This function will be called every time that an image
// cannot be loaded because of an error. Same remark as above
// regarding parameters.
}

var aimgPreloaded = new Array();
aimgPreloaded[ 0 ] = new Image();
aimgPreloaded[ 0 ].onload = onLoad; // The functions defined above
aimgPreloaded[ 0 ].onerror = onError;
aimgPreloaded[ 0 ].src = "images/image0.jpg";
aimgPreloaded[ 1 ] = new Image();
aimgPreloaded[ 1 ].onload = onLoad;
aimgPreloaded[ 1 ].onerror = onError;
aimgPreloaded[ 1 ].src = "images/image1.jpg";
....

When you want to use the image, for example for display in the webpage,
you do this:

var nImg = document.getElementById( "myImage" );
if ( nImg )
{
nImg.src = aimgPreloaded[ 1 ].src;
}

Because the image has been preloaded in the cache, it will be instantly
displayed.

I hope that gives you a basis of reflexion. Feel free to ask if
something is not clear.

Greetings,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Jun 15 '06 #2
Laurent Bugnion thanks. This seems to be an excellent start for me.
It will take me a little time to fully understand, after which I'm sure
I'll have more questions.

Thanks again.

www.richardfisher.com

Jun 15 '06 #3

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

Similar topics

1
3714
by: Dean | last post by:
First I've must say Im completly new in php scripting What I need to do is upload, resize pictures with path in database Here is theory of it, and plan of doing it Hope somebody can help me...
8
3910
by: Pjotr Wedersteers | last post by:
Hello, I tried to create an array with 1000 cells, keys 0 thru 999 using $myarr = array (1000); But this leads to an array of 1 cell with value 1000; Now I have a workable solution for...
2
9035
by: Alain. | last post by:
Good evenig to all, I use Access 2000 and i want to build a database for my photographs (jpeg files). All the 3 methods I saw make the base grow up. Is it a activeX control which could display...
2
17712
by: Charles Robinson III | last post by:
How do I make Access display pictures within a form view as linked objects, so that the pictures which I am linking to are displayed within the database? I have a seperate folder for pictures to be...
3
1576
by: Nathan | last post by:
Hi, I have a project with various forms that each include a number of picture boxes and panels with background images. When these forms first load, they kind of load in "pieces", with the...
13
2174
by: gooze | last post by:
Hello I am working on an applicaion that shows several pictures on a webpage. These pictures are saved in a MySQL DB as BLOB. I noticed, that the web server suffers in its performance by...
13
3240
by: Jim Carlock | last post by:
I have over a hundred pictures I would like to present. Is it practical to create and parse an array of pictures, picture paths, et al using server-side scripting to accomplish this? I...
3
1340
by: Shelly | last post by:
I want to display an array of pictures and I don't know beforehand how many there are in the array. I want there to be up to four pictures in a row (all the same size, of course), aligned to the...
1
3782
oranoos3000
by: oranoos3000 | last post by:
hi would you please help me i have a online shopping center that i show pictures of the my product in home page. in the InterExplorer pictures is shown correctly but in Firefox browser is shown...
0
7137
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
7349
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
7417
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
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5063
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...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
445
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...

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.