4

I want to preload an image with javascript, the method below seems simple enough but how can I check if the image has preloaded? Thanks Preloading images with jQuery

Community
  • 1
  • 1
Evanss
  • 23,390
  • 94
  • 282
  • 505

2 Answers2

3

I would suggest modifying the code to add on 'onerror' event handler to the img tags you are generating. and then handle any failures.

http://www.w3schools.com/jsref/event_onerror.asp

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
  • 2
    You can do the same for the onload of the img as well: http://www.w3schools.com/jsref/event_onload.asp – endyourif Oct 17 '11 at 14:41
1

Make a log entry in your image's "onload" event.

myImage.onload = function(){

   console.log("Image Loaded!");

}

If you're not familiar with chrome's developer tools or firefox's Firebug plugin, you should check them out. They make things like this a breeze.

Also, keep in mind that some browsers require the image.src attribute to be defined after the .onload event.

Daniel Szabo
  • 7,181
  • 6
  • 48
  • 65