RSS Feed

OSS Ramblings

http://www.ossramblings.com/preloading_images_without_javascript

 

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.



preload

Jean-Pierre's picture

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, 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>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Are you a BOT? What's this say?
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

 
 
 




Image 01 Image 02 Image 03