4

Possible Duplicate:
How to silently hide “Image not found” icon when src source image is not found

Is there any way, to disable default icon of not found image? If src give 404, Chrome shows small icon. Firefox doesn't show anything and I want the same in Chrome. Any solution for that?

Community
  • 1
  • 1
tunarob
  • 2,768
  • 4
  • 31
  • 48

2 Answers2

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

OR

You can use the onerror event in JavaScript to act when an image fails to load:

var img = document.getElementById("myImg");
img.onerror = function () { 
    this.style.display = "none";
}

In jQuery:

$("#myImg").error(function () { 
    $(this).hide(); 
});

Or for all images:

$("img").error(function () { 
    $(this).hide();
    // or $(this).css({visibility:"hidden"}); 
});

You should use visibility: hidden instead of .hide() if hiding the images might change the layout. Many sites on the web use a default "no image" image instead, pointing the src attribute to that image when the specified image location is unavailable.

Farzher
  • 13,934
  • 21
  • 69
  • 100
  • .error() shoud be better in $(window).load() or in $(document).ready()? – tunarob Sep 06 '12 at 16:21
  • `$(document).ready()` I think `$(window).load()` executes after images have loaded. You want to hook the event before they're loaded. – Farzher Sep 06 '12 at 16:25
0

There is a little solution using htaccess. you have to redirect to a 1x1 transparant png if the picture is not found. The only drawback is that if you explicitely define the width and height of the images, the image will still take the place it should. But nothing will be seen.

Salketer
  • 14,263
  • 2
  • 30
  • 58