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
  • 53,565
  • 76
  • 241
  • 413

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
  • 169,008
  • 28
  • 173
  • 236
-1

You should place the script inside document.ready:

    $(document).ready(function(){
       $('.avatar-50').error(function() {
          $(this).hide()
       })
    });
lvil
  • 4,326
  • 9
  • 48
  • 76