title: Fallback for 404 images
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Anyone has a suggestion?</p>
  
  <p><strong>Replace image using htaccess</strong></p>
  
  <pre><code>RewriteEngine on
  &lt;FilesMatch ".(jpg|png|gif)$"&gt;
  ErrorDocument 404 "/noimage.jpg"
  &lt;/FilesMatch&gt;
  </code></pre>
  
  <p><strong>Using JS</strong></p>
  
  <pre><code>&lt;img src="image.jpg" onerror="this.onerror=null;this.src='default.jpg'"&gt;
  </code></pre>
  
  <p><strong>Update: Working Version</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt; 
     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"
              ); 
           } 
      }); 
  }); 
  &lt;/script&gt;
  </code></pre>