View Full Version : A Javascript Image Preloading Bar


Ges
01-26-2007, 01:21 PM
POST 1

Hi to everyone,
There has been some questions recently about how images etc are loaded upon entry to a web site/page and the time taken.
Firstly, putting functions etc in the <head> section of your code ensures that they are loaded BEFORE they are called/manipulated from scripts/function withing the <body> section. This becomes very apparent when using large slide shows etc. Apart from the amount of images you may have, the size of them also plays an important part. The larger and more images you have then the longer it takes for your site/page to come up. This is the reason for some sites displaying a 'loading bar' to let you know what's going on!

Here is the code to do just that. It originated around 2 years ago ( I believe ), from Dynamic Drive but I've changed and updated it over time but credit must still go to the original author or DD for the skeleton ( of which it is based ). It now works in IE, FF and Opera.

This code would be your index.html. The only addition is the calling of the EXTERNAL Javascript called PROGRESSBAR.JS


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Preloading images</title>

<!-- call the external loading script -->
<script language="javascript" src="progressbar.js">
<!--
//-->
</script>

</head>
<body>

<center>
<h1>MY WEBSITE INDEX PAGE</h1>
</center>

</body>
</html>


As you can see the only part you would add to your code is;


<!-- call the external loading script -->
<script language="javascript" src="progressbar.js">
<!--
//-->
</script>


to your <head> section. PROGRESSBAR.JS is where you actually place the images. So if you have a slide show for instance, you would simply refernce them from the array in PROGRESS.JS.

THE NEXT POST IS THE PROGRESSBAR.JS ( due to forum size limits ).

Ges
01-26-2007, 01:29 PM
POST 2.

This is the PROGRESSBAR.JS. Copy this and call it progressbar.js. Put it in the same folder as your root or where your index file is. You can have as many images as you like. The bar will reflect the progress of the images loaded so far. Try it with some exremely large bit-map files ( .bmp ), to best see how it works.


// PROGRESSBAR.JS

// place all images into the array here
// all type are supported - jpg bmp png gif etc
// you can also set paths to your images

var imagenames=new Array
(
'pic1.jpg',
'pic2.jpg',
'pic3.gif',
'pic4.png',
'pic5.bmp',
'my_other_pics/anotherpic.jpg',
'../even_more_pics_folder/othrpics.jpg',
'../../and_more/thingy.bmp'
);

var yposition=250; // position of bar from top of browser
var loadedcolor='#6666ff' ; // loading color
var unloadedcolor='ffffff'; // not yet loaded color
var barheight=25; // bar height ( min 25 )
var barwidth=350; // bar width ( no limit really )
var bordercolor='000000'; // bar border color

// sort out the browsers

var NS4 = (navigator.appName.indexOf("Netscape")>=0 && parseFloat(navigator.appVersion) >= 4 && parseFloat(navigator.appVersion) < 5)? true : false;
var IE4 = (document.all)? true : false;
var NS6 = (parseFloat(navigator.appVersion) >= 5 && navigator.appName.indexOf("Netscape")>=0 )? true: false;

var imagesdone=false;
var blocksize=barwidth/(imagenames.length);
barheight=Math.max(barheight,25);
var loaded=0, perouter, perdone, images=new Array();
var txt=(NS4)?'<layer name="perouter" bgcolor="'+bordercolor+'" visibility="hide">' : '<div id="perouter" style="position:absolute; visibility:hidden; background-color:'+bordercolor+'">';
txt+='<table cellpadding="0" cellspacing="1" border="0"><tr><td width="'+barwidth+'" height="'+barheight+'" valign="center">';

if(NS4)
txt+='<ilayer width="100%" height="100%"><layer width="100%" height="100%" bgcolor="'+unloadedcolor+'" top="0" left="0">';
txt+='<table cellpadding="0" cellspacing="0" border="0"><tr><td valign="center" width="'+barwidth+'" height="'+barheight+'" bgcolor="'+unloadedcolor+'"><center><font color="'+loadedcolor+'" size="1" face="sans-serif">Loading Images - Please Wait.......</font></center></td></tr></table>';
if(NS4)
txt+='</layer>';
txt+=(NS4)? '<layer name="perdone" width="100%" height="'+barheight+'" bgcolor="'+loadedcolor+'" top="0" left="0">' : '<div id="perdone" style="position:absolute; top:1px; left:1px; width:'+barwidth+'px; height:'+barheight+'px; background-color:'+loadedcolor+'; z-index:100">';
txt+='<table cellpadding="0" cellspacing="0" border="0"><tr><td valign="center" width="'+barwidth+'" height="'+barheight+'" bgcolor="'+loadedcolor+'"><center><font color="'+unloadedcolor+'" size="1" face="sans-serif">Loading Images - Please Wait.......</font></center></td></tr></table>';
txt+=(NS4)? '</layer></ilayer>' : '</div>';
txt+='</td></tr></table>';
txt+=(NS4)?'</layer>' : '</div>';
document.write(txt);

// load the images depending uopn browser
function loadimages()
{
if(NS4)
{
perouter=document.perouter;
perdone=document.perouter.document.layers[0].document.perdone;
}
if(NS6)
{
perouter=document.getElementById('perouter');
perdone=document.getElementById('perdone');
}
if(IE4)
{
perouter=document.all.perouter;
perdone=document.all.perdone;
}

cliplayer(perdone,0,0,barheight,0);
window.onresize=setouterpos;
setouterpos();

for(n=0;n<imagenames.length;n++)
{
images[n]=new Image();
images[n].src=imagenames[n];
setTimeout('checkload('+n+')' ,n*100);
}
}

function setouterpos()
{
var ww=(IE4)? document.body.clientWidth : window.innerWidth;
var x=(ww-barwidth)/2;
if(NS4)
{
perouter.moveTo(x,yposition);
perouter.visibility="show";
}
if(IE4||NS6)
{
perouter.style.left=x+'px';
perouter.style.top=yposition+'px';
perouter.style.visibility="visible";
}
}

function dispbars()
{
loaded++;
cliplayer(perdone, 0, blocksize*loaded, barheight, 0);
if(loaded>=imagenames.length)setTimeout('hideperouter()', 800);
}

function checkload(index)
{
(images[index].complete)? dispbars() : setTimeout('checkload('+index+')', 100);
}

function hideperouter()
{
(NS4)? perouter.visibility="hide" : perouter.style.visibility="hidden";
imagesdone=true;
}

function cliplayer(layer, ct, cr, cb, cl)
{
if(NS4)
{
layer.clip.left=cl;
layer.clip.top=ct;
layer.clip.right=cr;
layer.clip.bottom=cb;
}
if(IE4||NS6)
layer.style.clip='rect('+ct+' '+cr+' '+cb+' '+cl+')';
}
window.onload=loadimages;


Hope this proves useful.

Regards,
Ges.

onigiri
01-30-2007, 05:35 PM
Moved to UST. :)