<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Using jQuery to Load Alternate Images</title>
	<atom:link href="http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/</link>
	<description>Anuj @ ColdFusion, Flex and some more....</description>
	<lastBuildDate>Wed, 10 Mar 2010 09:31:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Peter</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3519</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Tue, 12 Jan 2010 20:03:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3519</guid>
		<description>@Denis:

It will actually load an extra image for EVERY image in the document, not only for the broken ones.

However, images are cached quite efficiently (with default settings), so the offline.jpg will only be downloaded once for your whole site, even if the user revisits a few days later.

I would guess the resource usage in the browser would also be minimal, since the browser knows it&#039;s the same image being reused everywhere.

I would definitely guess the resource usage is (far) less using one CSS command (which is handled internally by the browser) than looping through all the images in the document using JavaScript.</description>
		<content:encoded><![CDATA[<p>@Denis:</p>
<p>It will actually load an extra image for EVERY image in the document, not only for the broken ones.</p>
<p>However, images are cached quite efficiently (with default settings), so the offline.jpg will only be downloaded once for your whole site, even if the user revisits a few days later.</p>
<p>I would guess the resource usage in the browser would also be minimal, since the browser knows it&#8217;s the same image being reused everywhere.</p>
<p>I would definitely guess the resource usage is (far) less using one CSS command (which is handled internally by the browser) than looping through all the images in the document using JavaScript.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Denis</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3518</link>
		<dc:creator>Denis</dc:creator>
		<pubDate>Tue, 12 Jan 2010 15:37:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3518</guid>
		<description>@Peter: That would seem to work but would it not then load that extra image for every image broken or not? This might seem like a small issue as it&#039;s just one error image but if you have different error images and or a complex page redraw time etc would be effected heavily?</description>
		<content:encoded><![CDATA[<p>@Peter: That would seem to work but would it not then load that extra image for every image broken or not? This might seem like a small issue as it&#8217;s just one error image but if you have different error images and or a complex page redraw time etc would be effected heavily?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anuj Gakhar</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3454</link>
		<dc:creator>Anuj Gakhar</dc:creator>
		<pubDate>Tue, 10 Nov 2009 11:24:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3454</guid>
		<description>Peter, that is a good trick specially if you dont use any transparent images. thanks for the tip.</description>
		<content:encoded><![CDATA[<p>Peter, that is a good trick specially if you dont use any transparent images. thanks for the tip.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3453</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Tue, 10 Nov 2009 09:30:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3453</guid>
		<description>You could also forget all the JavaScript and go the CSS way:

IMG { background-image: url(&#039;offline.jpg&#039;); }

Though this might give weird results if you use GIF or PNG images with transparent parts.</description>
		<content:encoded><![CDATA[<p>You could also forget all the JavaScript and go the CSS way:</p>
<p>IMG { background-image: url(&#8216;offline.jpg&#8217;); }</p>
<p>Though this might give weird results if you use GIF or PNG images with transparent parts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anuj Gakhar</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3385</link>
		<dc:creator>Anuj Gakhar</dc:creator>
		<pubDate>Mon, 22 Jun 2009 08:03:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3385</guid>
		<description>@Rolf, this script would be placed after you have loaded the jQuery library and its ok to place it in the head section or an external .js file. It will only run when the document is loaded.</description>
		<content:encoded><![CDATA[<p>@Rolf, this script would be placed after you have loaded the jQuery library and its ok to place it in the head section or an external .js file. It will only run when the document is loaded.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rolf</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3384</link>
		<dc:creator>Rolf</dc:creator>
		<pubDate>Mon, 22 Jun 2009 04:35:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3384</guid>
		<description>Forgive my ignorance when it comes to javascript, but what is the optimal code now?
And where should I place it, in the head section I suppose? Or can I put it in my external .js doc and if so, can I call the doc in the usual way or is a special command needed for this code?
Thanks in advance for your time.</description>
		<content:encoded><![CDATA[<p>Forgive my ignorance when it comes to javascript, but what is the optimal code now?<br />
And where should I place it, in the head section I suppose? Or can I put it in my external .js doc and if so, can I call the doc in the usual way or is a special command needed for this code?<br />
Thanks in advance for your time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anuj Gakhar</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3217</link>
		<dc:creator>Anuj Gakhar</dc:creator>
		<pubDate>Tue, 17 Mar 2009 09:19:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3217</guid>
		<description>@Dan, Thanks for the explanation. I will keep that in mind and if I notice anything different, I will post here. Cheers.</description>
		<content:encoded><![CDATA[<p>@Dan, Thanks for the explanation. I will keep that in mind and if I notice anything different, I will post here. Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan G. Switzer, II</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3216</link>
		<dc:creator>Dan G. Switzer, II</dc:creator>
		<pubDate>Mon, 16 Mar 2009 21:57:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3216</guid>
		<description>@Anuj:

The problem isn&#039;t with wanting to use the onerror--which is the correct event to monitor if an image loaded or not.

The problem is that $(document).ready() is not guaranteed to fire before all images have tried to load.

Where I can see there being issues is if you&#039;re running some other jQuery plug-ins during onready that are running before the fixBroken() plugin. 

I just seem to recall someone trying to implement a similar fix to yours, but was having some issues because his code was run after the browser had attempted to load the images.</description>
		<content:encoded><![CDATA[<p>@Anuj:</p>
<p>The problem isn&#8217;t with wanting to use the onerror&#8211;which is the correct event to monitor if an image loaded or not.</p>
<p>The problem is that $(document).ready() is not guaranteed to fire before all images have tried to load.</p>
<p>Where I can see there being issues is if you&#8217;re running some other jQuery plug-ins during onready that are running before the fixBroken() plugin. </p>
<p>I just seem to recall someone trying to implement a similar fix to yours, but was having some issues because his code was run after the browser had attempted to load the images.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anuj Gakhar</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3215</link>
		<dc:creator>Anuj Gakhar</dc:creator>
		<pubDate>Mon, 16 Mar 2009 21:18:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3215</guid>
		<description>@Dan, that sounds like a decent solution to the problem. I like the idea of using complete and naturalWidth to determine if the image is loaded or not. However, I am not sure if this is any better than onerror event as that is an internal event and I had no problems with onerror in any browser.</description>
		<content:encoded><![CDATA[<p>@Dan, that sounds like a decent solution to the problem. I like the idea of using complete and naturalWidth to determine if the image is loaded or not. However, I am not sure if this is any better than onerror event as that is an internal event and I had no problems with onerror in any browser.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan G. Switzer, II</title>
		<link>http://www.anujgakhar.com/2009/03/16/using-jquery-to-load-alternate-images/comment-page-1/#comment-3214</link>
		<dc:creator>Dan G. Switzer, II</dc:creator>
		<pubDate>Mon, 16 Mar 2009 20:13:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.anujgakhar.com/?p=347#comment-3214</guid>
		<description>Sorry, I should have paid more attention to the code. I glanced at it quickly.

However, there&#039;s still a potential problem with this code.

The timing of when $(document).ready() fires isn&#039;t an exact science. There&#039;s no way to guarantee it fires *before* images load. This leaves it open to weird bugs.

A quick search turned up this solution:

$(window).load(function() {
  $(&#039;img&#039;).each(function() {
    if (!this.complete &#124;&#124; typeof this.naturalWidth == &quot;undefined&quot; &#124;&#124; this.naturalWidth == 0) {
      // image was broken, replace with your new image
      this.src = &#039;http://www.tranism.com/weblog/images/broken_ipod.gif&#039;;
    }
  });
});

The idea they&#039;re using is to check the &quot;complete&quot; property which should be set to true/false if the image loaded. However, since Gecko doesn&#039;t support that it falls back to check naturalWidth (a Gecko property) to see if it&#039;s not defined or set to an invalid size (0).</description>
		<content:encoded><![CDATA[<p>Sorry, I should have paid more attention to the code. I glanced at it quickly.</p>
<p>However, there&#8217;s still a potential problem with this code.</p>
<p>The timing of when $(document).ready() fires isn&#8217;t an exact science. There&#8217;s no way to guarantee it fires *before* images load. This leaves it open to weird bugs.</p>
<p>A quick search turned up this solution:</p>
<p>$(window).load(function() {<br />
  $(&#8216;img&#8217;).each(function() {<br />
    if (!this.complete || typeof this.naturalWidth == &#8220;undefined&#8221; || this.naturalWidth == 0) {<br />
      // image was broken, replace with your new image<br />
      this.src = &#8216;http://www.tranism.com/weblog/images/broken_ipod.gif&#8217;;<br />
    }<br />
  });<br />
});</p>
<p>The idea they&#8217;re using is to check the &#8220;complete&#8221; property which should be set to true/false if the image loaded. However, since Gecko doesn&#8217;t support that it falls back to check naturalWidth (a Gecko property) to see if it&#8217;s not defined or set to an invalid size (0).</p>
]]></content:encoded>
	</item>
</channel>
</rss>
