Skip to main content

jQuery Preloading External Images

By Sam Deering



Free JavaScript Book!

Write powerful, clean and maintainable JavaScript.

RRP $11.95

This is how you might go about preloading images into the browser cache (Caching External Images) that will be later inserted into the page. This method would be useful only if you have many images and no control over the load order of the image containers. For example if you are grabbing the images for new tweets and then showing new tweets with user profile pictures. Before image loads from external server (Twitter in this example) there will be an ugly missing image icon.

Broken images…


After a few seconds the images appear…


So lets preload the external images…

So when the tweets are loaded in (whichever method your using) you can add this snippet to cache the user profile image.

//cache the user img
$('img').hide().appendTo('body').one('load', function() {
   console.log('Image: '+$(this).attr('src')+' is cached...');

Output in Firebug console

Output in Firebug net panel

That’s it your images should onw preload and not appear broken! :)

Related Articles

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

New books out now!

Learn the basics of programming with the web's most popular language - JavaScript

A practical guide to leading radical innovation and growth.

Integromat Tower Ad