Page 1 of 1

Preloaded Images

Posted: Fri Jan 08, 2010 11:18 pm
by tonyeveland
Hello there,

I use javascript to preload images as show below. The problem is that every time a page is refreshed, it preloads again. Is there any way to get an image into memory that stays there forever without any more preloading?


<script type='text/javascript'>
if (document.images) {
img001 = new Image(); img001.src = 'images/background.jpg ';
img002 = new Image(); img002.src = 'images/banner.jpg ';
}
</script>

Re: Preloaded Images

Posted: Mon Jan 11, 2010 9:22 am
by Reviresco
The "complete" property:

complete Property
The complete property is read-only and returns a Boolean value indicating whether or not the browser has completed loading the image.

Syntax: Image.complete

Code: Select all

 
if(img001.complete == false) {
   img001.src = 'images/background.jpg';
}
 

Re: Preloaded Images

Posted: Mon Jan 11, 2010 2:26 pm
by kaszu
to get an image into memory
no, but if response headers are correct, then they will be saved in browsers cachem, but it's fast
The problem is that every time a page is refreshed, it preloads again
Are you sure it preloads from server and not from cache? How did you checked it?

Sorry, but Reviresco solution won't work, because there's nothing to preload, see

Code: Select all

img001 = new Image();  //Creates blank image
if(img001.complete == false) {  //Image doesn't have src, so it's true and 'images/background.jpg' won't be preloaded