Tony's ramblings on Open Source Software, Life and Photography

Preloading Images Without Javascript

Here's a cool little trick I'm using on this site to preload the images for the hover-over replacements (today, search, archives). Without this trick, when you first hover over any of those links there's a delay before the image appears while the browser fetches it.

First, put this in your CSS file:

div#preloaded-images {
   position: absolute;
   overflow: hidden;
   left: -9999px; 
   top: -9999px;
   height: 1px;
   width: 1px;
}

Next, at the end of your HTML document, place the following, with one line for each image you want to preload:

<div id="preloaded-images">
   <img src="http://mydomain.com/image-01.png" width="1" height="1" alt="Image 01" />
   <img src="http://mydomain.com/image-02.png" width="1" height="1" alt="Image 02" />
   <img src="http://mydomain.com/image-03.png" width="1" height="1" alt="Image 03" />
</div>

This worked for me when for some reason Javascript preloading would not.


Categories:
Jean-Pierre's picture

preload

Congratulation Tony. Your method is the good one.
I think (but I am not an expert) that, in Javascript, when defining an object "image" with the attribute "source" giving the address of the image, IE does a load of the picture but Firefox not.

With your method, the "load" is forced both for IE and for Firefox

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <p>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for preventing automated spam submissions. It is case sensitive.
Image CAPTCHA
Enter the characters shown in the image.