1

How do I check if all images of appended HTML are loaded? The code looks like this:

$.ajax({
    url: "../api/listings",
    type: 'GET',
    success: function (html) {
        $('#container').append(html);
    };
});

$newItems contains some IMG's. I need to run a function after they are loaded. I tried adding this to success function: $('#container').on('load', function(){console.log('hello world!')}); but it didn't work

Edmund Sulzanok
  • 1,883
  • 3
  • 20
  • 39

3 Answers3

1
$.ajax({
    url: "../api/listings",
    type: 'GET',
    success: function (html) {
        $('#container').append(html);
        $('#container img').on('load', function(){console.log('images loaded')});
    };
});
Edmund Sulzanok
  • 1,883
  • 3
  • 20
  • 39
0

Try something like this. Create a global variable like this var allImagesLoaded = true; then on the appended HTML add an onerror handler which set the global variable to false. Check it out after a time-out or something like that

naveen
  • 53,448
  • 46
  • 161
  • 251
0

Take a look at this library : https://github.com/desandro/imagesloaded/

It allows you to listen for loaded images in a particular element.

Such as :

$('#container').imagesLoaded(function(){
 console.log("All images loaded!");
})
Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34