471,872 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,872 software developers and data experts.

Problem Pre-Loading Images Using Javascript

I am trying to get the following code to pre-load some images before re-dirrecting the user. The problem is that the onError event gets called for all of the images immediately. I've tripple checked that the images do in fact exist at the specific url, so I can't figure out why the onError is being called. The script is currently posted on http://www.mkcustomdesign.com/index.html.


<script type="text/javascript">

var imagesToLoad = 0;
var imagesLoaded = 0;

function loadImages(){


images = new Array("loading.jpg", "row1.jpg", "index_02.jpg", "index_03.jpg", "index_03.jpg", "index_04.jpg", "index_05.jpg",
"index_06.jpg", "mkcustom.jpg", "spacerrow.jpg", "box_1_1_table.jpg", "box_1_2_chair.jpg",
"box_1_3_bed.jpg","side_1_right.jpg", "side_1_left.jpg", "row3.jpg", "side_2_left.jpg",
"box_2_1_cabinets.jpg" , "box_2_2_railing.jpg", "box_2_3_misc.jpg", "side_2_right.jpg",
"bottom2.jpg" );

imagesToLoad = images.length;
for(i = 0; i < images.length; i++){
image = new Image();
image.onLoad = imageLoaded();
image.onError = error("images/" + images[i]);
image.src = "images/" + images[i];
}

}

function error(image){
alert('error with ' + image);
}

function imageLoaded(){
imagesLoaded++;
document.getElementById('loading').innerHTML = 'Loaded ' + imagesLoaded + "/" + imagesToLoad + ' images ';
if(imagesLoaded == imagesToLoad){
//document.location = "main.html";
}
}


</script>
Apr 25 '07 #1
1 1775
Logician
210 100+
Expand|Select|Wrap|Line Numbers
  1. image.onLoad = imageLoaded();
  2.         image.onError = error("images/" + images[i]);
An event handler is passed as a function address not a function call. I haven't tested this but try it:
Expand|Select|Wrap|Line Numbers
  1. image.onLoad = imageLoaded;
  2. image.onerror = new Function("error('images/' + images[i])") ;
  3.  
As for the loaded images indicator, you may not always get onload events when testing locally.
Apr 26 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Dan | last post: by
7 posts views Thread by Alan Illeman | last post: by
2 posts views Thread by Buck Turgidson | last post: by
2 posts views Thread by Leszek | last post: by
1 post views Thread by Leszek | last post: by
7 posts views Thread by effendi | last post: by
12 posts views Thread by Vadim Guchenko | last post: by
1 post views Thread by alain | last post: by
7 posts views Thread by Kim | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

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.