0

Code:

<img src="//www.gravatar.com/avatar/881575a2c5c2c37d6de9166e384ad596?d=gravatar_default&amp;s=50&amp;r=G" class="avatar user-40-avatar avatar-50 photo" width="50" height="50" alt="Profile picture of eagink@gmail.com">

$('.avatar-50').error(function() {
  $(this).hide()
})

(No image is being hidden).

As you can see some of the images are not loading:

enter image description here

Why is this? And how to detect those unloaded pictures?

Live site: http://www.chineselearnonline.com/members/

alexchenco
  • 50,467
  • 73
  • 227
  • 400

2 Answers2

2

You need to assign the error handler outside the onload event, BEFORE the image tags since now you assign the error handler AFTER all images have loaded

Alternative

<img onerror="this.style.display='none'" ...>

or

<img onerror="this.src='blank.gif'" ...>

or after the fact (taken from here):

$(function() {
  $(".avatar-50").each(function() {
    if (!this.complete || (typeof this.naturalWidth !== "undefined" && this.naturalWidth === 0)) {
      $(this).hide();
   }
  });
});
Community
  • 1
  • 1
mplungjan
  • 155,085
  • 27
  • 166
  • 222
-1

You should place the script inside document.ready:

    $(document).ready(function(){
       $('.avatar-50').error(function() {
          $(this).hide()
       })
    });
lvil
  • 4,209
  • 8
  • 46
  • 73