Forum Moderators: open

Message Too Old, No Replies

background image loading

beyond preloading, maybe XMLHTTPRequest

         

Mutley2003

9:24 am on May 4, 2005 (gmt 0)

10+ Year Member



Hi everyone,
I have a situation in which there are a LOT of images (maybe 100) to be downloaded on a page, but they are not all visible at once. Basically they are organized in a "pile" with one beneath the other.

As soon as each image is loaded I would like the user to be able to interact with the "pile".. eg click on a button and shuffle through the images that have been loaded to date.

I am aware of techniques for preloading images and also of the trick for getting them in a script block (with a hidden style) just before /body. However, I don't get any user interactivity until the script has finished executing (and ALL the images loaded).

So I want to get some technique for allowing the page to be complete enough for users to interact with it even though all the images have not been loaded.

Is there some way I can use setTimeout to do the image loading in the background? or XMLHTTPRequest?

thanks

Bernard Marx

2:13 pm on May 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Start off with an empty array. Whether you are using preloading, or layered actual image elements in the document, you can use each image's onload event handler to make the image push a reference to itself into the array when it has loaded. This way, you'll have a consistent record of the currently available images.

Mutley2003

10:24 pm on May 4, 2005 (gmt 0)

10+ Year Member



I understand the array approach, but nevertheless (even if I put the appropriate script at the bottom of the page) I cannot press a button on the page until all images have finished loading. But it seemed to me that some sort of timer loop with setTimeout() might help..

Bernard Marx

8:13 am on May 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I cannot press a button on the page until all images have finished loading

I don't understand this. Could you post the relevant code?

Mutley2003

10:50 pm on May 5, 2005 (gmt 0)

10+ Year Member



<html>
<head>
<style type="text/css">
.hiddenPic {display:none;}
</style>

<script type="text/javascript" src="wz_dragdrop.js"></script>
<META HTTP-EQUIV="imagetoolbar" CONTENT="no">
</head>
<body>
<h1> hello there fred </h1>
<p>
<img id="myimg1" src="images/England_4.jpg" height=200 width=200>
</p>
<script>
allImagesLoaded = false;
imgNum=0;
var imageSet =new Array();

function changeImage(){
var anImage = document.getElementById('myimg1');
if (allImagesLoaded) {
anImage.src =imageSet[imageSet.length-1].src;}
else
{document.body.bgColor = "Yellow";}
}
</script>
<button onclick="changeImage()">
click me to change image
</button>
<!-- ======================================================
preload images need for site by using css.. this will
use the browser in background mode to fetch stuff,
but these will never be displayed ie the user experience
is unaffected
====================================================== -->
<script>
function loadImage2Cache(imgName) {
imageSet[imgNum] = new Image();
imageSet[imgNum].src="images/"+imgName;
self.defaultStatus = imgName;
self.status = imgName;
imgNum++;
}
loadImage2Cache("Australia_2.jpg");
loadImage2Cache("Australia_3.jpg");
loadImage2Cache("Australia_4.jpg");
loadImage2Cache("England_5.jpg");
loadImage2Cache("England_6.jpg");
loadImage2Cache("EasterIsland_1.jpg");
loadImage2Cache("EasterIsland_2.jpg");
loadImage2Cache("Bali1.jpg");
loadImage2Cache("Bali2.jpg");
loadImage2Cache("Bali3.jpg");
loadImage2Cache("Capri2.jpg");
loadImage2Cache("Bali2.jpg");
allImagesLoaded = true;
</script>

<!-- ======================================================


<img class="hiddenPic" src="images/Australia_2.jpg" >
<img class="hiddenPic" src="images/Australia_3.jpg" >
<img class="hiddenPic" src="images/Australia_4.jpg" >
<img class="hiddenPic" src="images/England_5.jpg" >
<img class="hiddenPic" src="images/England_6.jpg" >
<img class="hiddenPic" src="images/EasterIsland_1.jpg" >
<img class="hiddenPic" src="images/EasterIsland_2.jpg" >
<img class="hiddenPic" src="images/Bali1.jpg" >
<img class="hiddenPic" src="images/Bali2.jpg" >
<img class="hiddenPic" src="images/Bali3.jpg" >
<img class="hiddenPic" src="images/Capri2.jpg"
onload='alert("done");'>

====================================================== -->

<!-- ======================================================

onload='document.body.bgColor="Red";'>
====================================================== -->

</body>
</html>