Skip to content

Instantly share code, notes, and snippets.

@amclean
Created January 23, 2013 21:24
Show Gist options
  • Save amclean/4613608 to your computer and use it in GitHub Desktop.
Save amclean/4613608 to your computer and use it in GitHub Desktop.
The right way to preload images that allows for onload event firing even if IE has cached image.
var imageLoadEvent = function(){
console.log('image ready');
}
var $newImg = this.$(new Image()),
src = 'blah.png';
// using onreadstatechange to deal with cached stuff in IE
$newImg.bind('load readystatechange', function(e){
if(this.complete || (this.readyState == 'complete' && e.type == 'readystatechange')) imageLoadEvent();
});
$newImg.attr('src', src);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment