Check if an image is loaded (no errors) with jQuery

前端 未结 15 1356
挽巷
挽巷 2020-11-21 20:45

I\'m using JavaScript with the jQuery library to manipulate image thumbnails contained in a unordered list. When the image is loaded it does one thing, when an error occurs

15条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 21:06

    This snippet of code helped me to fix browser caching problems:

    $("#my_image").on('load', function() {
    
        console.log("image loaded correctly"); 
    }).each(function() {
    
         if($(this).prop('complete')) $(this).load();
    });
    

    When the browser cache is disabled, only this code doesn't work:

    $("#my_image").on('load', function() {
         console.log("image loaded correctly"); 
    })
    

    to make it work you have to add:

    .each(function() {
         if($(this).prop('complete')) $(this).load();
    });
    

提交回复
热议问题