473,386 Members | 1,721 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,386 software developers and data experts.

Preload next number (image) in array on a click of a mouse

Here's my problem if anyone could be so kind to help me out. I assume
it's quite easy for an experienced programmer but I'm not one of them
:)

I have a JavaScript code that:
- displays multiple images (+40) in a single image placeholder on ONE
page
- pre-loads multiple images
- on a click of a mouse changes to the next image
- on a click of a mouse changes to the previous image

PROBLEM:
1. all the images pre-load and eat up the bandwith
2. The images are not put in the browser's cache for easy loading
later on

SOLUTIONS REQUESTED:
1. that when I click "next" the next image is displayed PLUS the image
AFTER that one is loaded

2. If possible, that the images be loaded in the browsers cache.

Thank's for your time...

THE JAVASCRIPT CODE:

<script language="Javascript">
<!--

function switchImg(from,to){
document[from].src = eval(to + ".src");
}

count = 0;
MyImages = new Array();
MyImages[0]=new Image();
MyImages[0].src='arnor00.jpg';
MyImages[1]=new Image();
MyImages[1].src='arnor01.jpg';
MyImages[2]=new Image();
MyImages[2].src='arnor02.jpg';
MyImages[3]=new Image();
MyImages[3].src='arnor03.jpg';
MyImages[4]=new Image();
MyImages[4].src='arnor04.jpg';
MyImages[5]=new Image();
MyImages[5].src='arnor05.jpg';
MyImages[6]=new Image();
MyImages[6].src='arnor06.jpg';
MyImages[7]=new Image();
MyImages[7].src='arnor07.jpg';
MyImages[8]=new Image();
MyImages[8].src='arnor08.jpg';
MyImages[9]=new Image();
MyImages[9].src='arnor09.jpg';
MyImages[10]=new Image();
MyImages[10].src='arnor10.jpg';
function loadImg(from,to){
document[from].src = to;
}

function Next(){
if (count < MyImages.length - 1){
count = count + 1;
document.paintings.src = MyImages[count].src;

}
else {
count = 0;
document.paintings.src = MyImages[count].src;
}

}
function Back(){
if (count > 0){
count = count - 1;
document.paintings.src = MyImages[count].src;

}
else {
count = 4;
document.paintings.src = MyImages[count].src;
}

}
//-->
</script>
Jul 20 '05 #1
2 4018
as*****@finlina.is (Wonko) writes:
Here's my problem if anyone could be so kind to help me out. I assume
it's quite easy for an experienced programmer but I'm not one of them
:)

I have a JavaScript code that:
- displays multiple images (+40) in a single image placeholder on ONE
page
- pre-loads multiple images
- on a click of a mouse changes to the next image
- on a click of a mouse changes to the previous image

PROBLEM:
1. all the images pre-load and eat up the bandwith 2. The images are not put in the browser's cache for easy loading
later on
That sounds mysterious.
Prefetching/pre-loading images off-screen only serves one purpose:
to get them into the browser's cache. If they are not in the cache
later, it is either because the cache is too small, or because
they somehow told the browser that they should not be cached for long.
SOLUTIONS REQUESTED:
1. that when I click "next" the next image is displayed PLUS the image
AFTER that one is loaded

2. If possible, that the images be loaded in the browsers cache.

Some code:
---
<script type="text/javascript">

var MyImages = ['arnor00.jpg', 'arnor01.jpg', 'arnor02.jpg',
'arnor03.jpg', 'arnor04.jpg', 'arnor05.jpg',
'arnor06.jpg', 'arnor07.jpg', 'arnor08.jpg',
'arnor09.jpg', 'arnor10.jpg'];
var numURLs = MyImages.length;
var count = 0;

var cacher = new Image();
cacher.src = MyImages[1]; // expects at least two URLs.

var prevCacher = newImage;
prevCacher.src = MyImages[numURLs - 1];

function Next(){
count = (count+1)%numURLs;
document.images['paintings'].src=MyImages[count];
var next = (count+1)%numURLs;
cacher.src=MyImages[next];
}

function Prev(){
count = (count-1)%numURLs;
document.images['paintings'].src=MyImages[count];
var prev = (count-1)%numURLs;
cacher.src=MyImages[prev];
}

</script>
---
Try it, but be warned that it is untested.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<sm**********@hotpop.com>...
as*****@finlina.is (Wonko) writes:

Thanks for your help LRN...
That sounds mysterious.
Prefetching/pre-loading images off-screen only serves one purpose:
to get them into the browser's cache. If they are not in the cache
later, it is either because the cache is too small, or because
they somehow told the browser that they should not be cached for long.
Yes I agree that this is mysterious. In the code you gave me in the
"next function" it loads two image, next image and next image+1. but
it does that EVERY time, which is quite annoying. Can the hosting
provider (MediaTemple) have anything to say (configure) about that?
The page in question requires login as it's on a secure folder, does
that make a difference?
Try it, but be warned that it is untested.
/L


I did and there were minor errors in it, like you said... not tested.
Here is a functional code without errors if anyone is interested but I
couldn't verify that the preload works properly as it loads every time
on my page.

Changes:
"var PrevCacher" is out, shouldn't be neccessary if the image is
already in the cache from the "var chacher"

IF sentence added to the "back function" as it didn't display any
image if the first image in the series was loaded. now nothing
happens.

<script type="text/javascript">
<!--
var MyImages = ['arnor00.jpg', 'arnor01.jpg', 'arnor02.jpg',
'arnor03.jpg', 'arnor04.jpg', 'arnor05.jpg',
'arnor06.jpg', 'arnor07.jpg', 'arnor08.jpg',
'arnor09.jpg', 'arnor10.jpg', 'arnor11.jpg',
'arnor12.jpg', 'arnor13.jpg', 'arnor14.jpg',
'arnor15.jpg', 'arnor16.jpg', 'arnor17.jpg',
'arnor18.jpg', 'arnor19.jpg', 'arnor20.jpg',
'arnor21.jpg', 'arnor22.jpg', 'arnor23.jpg',
'arnor24.jpg', 'arnor25.jpg', 'arnor26.jpg',
'arnor27.jpg', 'arnor28.jpg', 'arnor29.jpg',
'arnor30.jpg', 'arnor31.jpg', 'arnor32.jpg',
'arnor33.jpg', 'arnor34.jpg', 'arnor35.jpg',
'arnor36.jpg', 'arnor37.jpg', 'arnor38.jpg',
'arnor39.jpg', 'arnor40.jpg',];
var numURLs = MyImages.length;
var count = 0;

var cacher = new Image();
cacher.src = MyImages[1];

function Next(){
count = (count+1)%numURLs;
document.images['paintings'].src=MyImages[count];
var next = (count+1)%numURLs;
cacher.src=MyImages[next];
}

function Back(){
if (count > 0){
count = (count-1)%numURLs;
document.images['paintings'].src=MyImages[count];
var prev = (count-1)%numURLs;
cacher.src=MyImages[prev];
}

else {
count = 0;
document.images['paintings'].src=MyImages[count];
}
}
//-->
</script>

and the links are of course:
<a href="javascript:Back ()">some text or image</a>
<a href="javascript:Next ()">some text or image>/a>

Thanks again...
Jul 20 '05 #3

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

Similar topics

4
by: Csaba2000 | last post by:
I want to be able to programatically click on the center of an <INPUT type=image ...> element (I only care about IE 5.5+). This should work regardless of whether IE has focus. Normally you would...
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...
3
by: Bob | last post by:
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: ...
1
by: Harod Ruthgar | last post by:
var image = new Image(); image.src="pathToTheFile"; The above code is called 25 times (with different pathToTheFile) within a loop. I manually checked the cache directory and not all images...
15
by: Tony Gahlinger | last post by:
I'm using my browser (Mozilla/5.0 Linux i686 Gecko/20031007 Firebird/0.7) to do some client-side image processing. I want to capture the sequence of coordinates a user clicks on in xxx.jpg in the...
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...
4
by: shapper | last post by:
Hello, I think to preload an image I should us something like: img = new Image(); img.src = 'images/img.jpg'; Could someone tell me how to create a loop which would preload a list of...
2
by: matt9807 | last post by:
The following is a script that I have written that preloads images for an image gallery. The problem is that the images only preload in Safari, other browsers load each image when it is called. Any...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.