Javascript really has no way to check the actual physical cache of images. It can only test if a given image object is cached. Other than images that are listed as the src attribute of an image tag, the only other type of javascript image object is the new Image() object. Each time you create one of these, it has to be loaded to be tested.
You can create a session cookie that will tell the browser if it has already done this or not and to skip it if it has. If a user has cookies disabled, they will get the loadbar each time. If the user clears their cookies while viewing the site they may (depending upon how the browser handles session cookies when its clear cookies button is pressed, some retain session cookies) have the loadbar again but, in most cases, only one extra time, as long as they don't clear cookies again.
If the user clears their cache but not their cookies, the images will no longer be preloaded. This is not something people usually do but, on occasion, it will happen.
To set this up, add this fairly good cookie unit from quirksmode.org and this variable declaration to the external script, just after the //DO NOT EDIT BEYOND THIS POINT line:
Code:
//DO NOT EDIT BEYOND THIS POINT
function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name)
{
createCookie(name,"",-1);
}
var needed=readCookie('loaded')=='yes'? 0 : 1
Then before the document.write line, add this test (addition red):
Code:
if(needed)
document.write(txt);
And add this test at the beginning of the function loadimages():
Code:
function loadimages(){
if(!needed)
return;
if(NS4){
Finally, add this command to the function hideperouter():
Code:
function hideperouter(){
(NS4)? perouter.visibility="hide" : perouter.style.visibility="hidden";
imagesdone=true;
createCookie('loaded','yes')
}
That's it, when all instances of the browser are closed, the cookie will be cleared but, as long as the session continues, preloading will not be repeated except under the circumstances mentioned at the top of this post.
Bookmarks