Hi,
I want to preload some images for my website so that the rollover effects are smooth. I have based this code on a javascript I found on the net:
[code]
var images = new Array();
var percent;
function preload()
{
var numImages = preload.arguments.length;
for( i = 0; i < numImages; i++ )
{
images[i] = new Image();
images[i].src = preload.arguments[i];
percent = parseInt(( i / numImages ) * 100);
}
}
Well… it just doesn’t work. The images which URL’s I pass as arguments to the function (which is called on onLoad) are NOT preloaded. The loop is passed as many times as there are images to load, but they aren’t actually loaded.
Any idea what’s wrong here?
<i>
</i>// preload.js
<!--
// Realised by ApacheJeff
// <a href="www.huntingground.freeserve.co.uk">www.huntingground.freeserve.co.uk</a>
images = new Array("img/ol1.jpg",
"img/ol2.jpg",
"img/ol3.jpg",
"img/ol_profile.jpg",
"img/ol_ring.jpg",
"img/ol_xbox.jpg",
"img/ol_cobe.jpg",
"img/bg_l.jpg",
"img/bg_r.jpg",
"img/bg_u.jpg",
"img/btt.jpg",
"img/mainbg.jpg",
"img/menu.jpg",
"img/top.jpg");
run_num = images.length;
img_total = images.length; // counter display
a_index = 0;
var preloaded = new Array();
function load_image()
{
preloaded[a_index] = new Image();
preloaded[a_index].onload = check_array();
preloaded[a_index].src = images[a_index];
document.write( "test" );
}
function check_array()
{
a_index++;
<i> </i>document.getElementById("info").innerHTML = "Loading " + img_total; // counter display
<i> </i>img_total--; // counter display
<i> </i>progress(); // progress bar function
<i> </i>if(a_index != run_num)
<i> </i>{
<i> </i> setTimeout("load_image()",10);
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> do_this(); // function to run
<i> </i> document.getElementById("info").innerHTML=""; // counter display
<i> </i> document.getElementById("progress_bar").style.visibility="hidden"; // progress bar display
<i> </i> document.getElementById("bg_layer").style.visibility="hidden"; // progress bar display
<i> </i>}
}
// progress bar display
Width=300; // width of bar
step_size=Width / images.length; // calculates step size
step=0;
function progress()
{
step+=(step_size*1);
document.getElementById("progress_bar").style.width=step;
}
function do_this()
{
alert("All the images have now been downloaded");
}
// add onload="load_image()" to the opening BODY tag
//-->
<i>
</i>// index.php
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
<script language="javascript" src="preload.js"></script>
<script language="javascript" src="imgswap.js"></script>
</head>
<body bgcolor="#E6E6E6" onLoad="load_image()">
...
</body>
</html>
//document.getElementById("info").innerHTML = "Loading " + img_total; // counter display
//img_total--; // counter display
//progress(); // progress bar function
if(a_index != run_num) {
setTimeout("load_image()",10);
}
else {
do_this(); // function to run
//document.getElementById("info").innerHTML=""; // counter display
//document.getElementById("progress_bar").style.visibility="hidden"; // progress bar display
//document.getElementById("bg_layer").style.visibility="hidden"; // progress bar display
}
0.1.9 — BETA 4.30