Feedback for 404 images

Every day, I automatically imported several XML feeds (Tradetracker, Daisycon, etc.) for an affiliate site using PHP. The feed contains products from all kinds of stores.

Everything works like a charm, with the exception of images. The images in the channels are just hotly connected to the image of the supplier. This works in most cases, but sometimes (for various reasons) the image no longer exists, is protected from hotlink, changes, etc. Etc. This leads to the fact that the "image was not found" in the browser, t look good.

I tried to solve this using htaccess, but for some reason it does not work. I googled and tried several htacces of "scripts", but no one was successful. I also tried JS in the image url, but this did not work. I prefer htaccess.

Anyone have a suggestion?

Replace image with htaccess

RewriteEngine on
<FilesMatch ".(jpg|png|gif)$">
ErrorDocument 404 "/noimage.jpg"
</FilesMatch>

Using JS

<img src="image.jpg" onerror="this.onerror=null;this.src='default.jpg'">

Update: working version

<script type="text/javascript"> 
   jQuery(window).load(function() { 
      jQuery("img").each(function(){ 
         var image = jQuery(this);             
         if(image.context.naturalWidth == 0 ||
         image.readyState == 'uninitialized'){    
            jQuery(image).unbind("error").attr(
                "src", "noimage.jpg"
            ); 
         } 
    }); 
}); 
</script>
+4
source share
1 answer

jQuery supports an error event for images - I'm not sure why your inline example does not work, but what about:

$('img').on('error', function() {
    $(this).attr('src', 'default.jpg');
});

, . .htaccess , , .

+5

Source: https://habr.com/ru/post/1524340/


All Articles