Preloaded Images

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
tonyeveland
Forum Newbie
Posts: 8
Joined: Thu Jul 14, 2005 11:03 am

Preloaded Images

Post 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>
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Preloaded Images

Post 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';
}
 
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Preloaded Images

Post 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
Post Reply