javascript - Fallback for 404 images - Stack Overflow

On a daily basis I automatically imported several XML-feeds (Tradetracker, Daisycon, etc.) for an affil

On a daily basis I automatically imported several XML-feeds (Tradetracker, Daisycon, etc.) for an affiliate site using PHP. The feeds contain products from all kinds of shops.

Everything works like a charm, with exception of the images. The images in the feeds are simply hotlinked to an image of the provider. This works in most of the cases, however sometimes (due to various reasons) the image doesn't exist anymore, is hotlink protected, is changed, etc. etc. This results in "image not found" in the browser, which doesn't look good.

I tried to solve this using htaccess, but for whatever reason, it doesn't work. I googled and tried several htacces "scripts", but none without success. I also tried JS in the image URL but this didn't work eiter. I do prefer htaccess.

Anyone has a suggestion?

Replace image using 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>

On a daily basis I automatically imported several XML-feeds (Tradetracker, Daisycon, etc.) for an affiliate site using PHP. The feeds contain products from all kinds of shops.

Everything works like a charm, with exception of the images. The images in the feeds are simply hotlinked to an image of the provider. This works in most of the cases, however sometimes (due to various reasons) the image doesn't exist anymore, is hotlink protected, is changed, etc. etc. This results in "image not found" in the browser, which doesn't look good.

I tried to solve this using htaccess, but for whatever reason, it doesn't work. I googled and tried several htacces "scripts", but none without success. I also tried JS in the image URL but this didn't work eiter. I do prefer htaccess.

Anyone has a suggestion?

Replace image using 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>
Share edited Jan 30, 2014 at 8:49 mat asked Jan 29, 2014 at 18:02 matmat 1,6391 gold badge16 silver badges25 bronze badges 1
  • Handy solution, well done! – OverlappingElvis Commented Jan 30, 2014 at 17:19
Add a ment  | 

1 Answer 1

Reset to default 6

jQuery supports the error event for images — I'm not sure why your inline example isn't working, but how about:

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

This also has the advantage of not needing to be added to each element. Your .htaccess solution doesn't work if your images are being hotlinked, since those would not be served from your site.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744908580a4600406.html

相关推荐

  • javascript - Fallback for 404 images - Stack Overflow

    On a daily basis I automatically imported several XML-feeds (Tradetracker, Daisycon, etc.) for an affil

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信