473,394 Members | 1,739 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,394 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 1871
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

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

Similar topics

2
by: Dan | last post by:
I'm trying to create a menu in javascript. I've created the bitmaps with text over the top of them. The problem i'm having is that the positions aren't correct. If I wrap the code in PRE tags,...
7
by: Alan Illeman | last post by:
How do I set several different properties for PRE in a CSS stylesheet, rather than resorting to this: <BODY> <PRE STYLE="font-family:monospace; font-size:0.95em; width:40%; border:red 2px...
2
by: Buck Turgidson | last post by:
I want to have a css with 2 PRE styles, one bold with large font, and another non-bold and smaller font. I am new to CSS (and not exactly an expert in HTML, for that matter). Is there a way to...
6
by: TomB | last post by:
Hello all, On the page located at http://deimos.curious.be/~dusk/lyrics.php#gotn I have put the lyrics text inside <pre> elements so that the UA renders the linebreaks in the code. Because...
2
by: Leszek | last post by:
I have a form with 5 dropdown lists I want to put all selected values to an array called $data so i wrote sometthing like this: for(i=0;i<5;i++){ echo"<select name=\"\$data\">"; echo"<option...
1
by: Leszek | last post by:
Hi. I'm using <pre><?php print_r($_POST); ?> </pre> and i'm getting this: Array (
7
by: effendi | last post by:
I am trying to figure this out. I have the following fuction, which takes value for a hidden field in a form and display the values on the browser. The code work fine except in Firefox/Opera,...
12
by: Vadim Guchenko | last post by:
Hello. I'm using the following code: <html> <head> <style type="text/css"> pre {display: inline;} </style> </head>
1
by: alain | last post by:
Hi, I have a problem with closures. I am trying to implement yet another design by contract decorator which would look like the following: <pre> def contract(f): def newf(*args, **kw):...
7
by: Kim | last post by:
Hi, why doesn't this work inside a loop, gives me the wrong "kontrollnummer" (increases it as the loop goes on) <% Dim kontrollnummer Dim raknare Dim Summa Dim referens referens =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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
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
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...

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.