Skip to content

Instantly share code, notes, and snippets.

@rbk
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbk/ce4411e742c2aa2cca0e to your computer and use it in GitHub Desktop.
Save rbk/ce4411e742c2aa2cca0e to your computer and use it in GitHub Desktop.
Pre load images
// Page this function a javascript array of image urls to preload them!
function pre_load_images(images){
for( var i=0; i < images.length; i++ ){
var img_new = document.createElement('img');
img_new.setAttribute( 'src', images[i] );
img_new.setAttribute( 'style', 'display:none;' );
document.body.appendChild(img_new)
}
}
/* Example */
// Array of image urls
var images = [
'http://domain.com/images/image1.png',
'http://domain.com/images/image2.png',
'http://domain.com/images/image3.png'
];
// Call function on page load passing in array
pre_load_images( images );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment