When pulling in content dynamically with jQuerys .load() method, the callback function fires when the request is complete and the HTML is pulled through onto your current page.
Is there a way to work out when the images contained in the new HTML are fully loaded?
Thanks!
As far as I know, this is not straight-forward. Some may suggest using the
.load()
event handler, but Jquery documentation has a number of caveats for using that (documentation):The only way that comes to mind is to use jQuery to pull the
img
elements out of your returned HTML, and use the javascriptImage
object to load the images. You can attach an.onload
event handler to theImage
object. You would then have to track when they are all loaded using something like a deferred object, or even a simple counter.Here is a demo that pulls out
img
objects and uses$.Deferred()
objects with a$.when()
to trigger when all images are loaded:http://jsfiddle.net/jtbowden/h4AMG/
In the demo, the content will only load after the images have loaded. You will need to change
$.load()
to$.ajax()
to have the control to do this.Here is a version that uses a basic counter to track loads:
http://jsfiddle.net/jtbowden/gnW3f/