<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BinaryKitten&#039;s Blog &#187; images</title>
	<atom:link href="http://binarykitten.me.uk/tag/images/feed" rel="self" type="application/rss+xml" />
	<link>http://binarykitten.me.uk</link>
	<description></description>
	<lastBuildDate>Tue, 07 Sep 2010 08:02:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>jQuery image Preloader Plus Callbacks</title>
		<link>http://binarykitten.me.uk/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html</link>
		<comments>http://binarykitten.me.uk/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html#comments</comments>
		<pubDate>Wed, 11 Feb 2009 21:56:26 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[jQuery Plugins]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[preloader]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=107</guid>
		<description><![CDATA[Hi all, After comments on the previous version by Roberto, I looked into creating callbacks within the code so that it can be used in the way that Roberto had outlined, also I used it as a way to improve my knowledge a little. After a while I got stuck and figured out a potential [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all, After comments on the previous version by Roberto, I looked into creating callbacks within the code so that it can be used in the way that Roberto had outlined, also I used it as a way to improve my knowledge a little.</p>
<p>After a while I got stuck and figured out a potential method thanks to ai-a in #Javascript on UnderNet, then I spoke to Remy Sharp on Twitter and always being impressed with the work he does on his site (<a href="http://www.jqueryfordesigners.com" target="_blank">http://www.jqueryfordesigners.com</a>) I asked politely if he would give my code a quick look over. He did so, and so much more besides.</p>
<p>So I present to you now v0.95 of the Image Pre-loader which is now Split into 3 functions (rather than the 2 from before)<br />
<span id="more-107"></span><br />
So the main functionality is the same as the one from <a href="http://binarykitten.jkrswebsolutions.co.uk/2009/01/06/jquery-image-preloader/" target="_blank">http://binarykitten.jkrswebsolutions.co.uk/2009/01/06/jquery-image-preloader/</a> but now has the following functions</p>
<ol>
<li><strong>$.preLoadImages</strong> &#8211; Preload the passed list of images, calling the passed call back function when all images are preloaded</li>
<li><strong>$.preLoadCSSImages</strong> &#8211; Preload all images found within the stylesheets of the document, then call the passed call back function when all the images are preloaded</li>
<li><strong>$.preLoadAllImages</strong> &#8211; Processes the stylesheet images and then if passed additional images, will process them too. When all are complete will call the passed callback function.</li>
</ol>
<p>Here&#8217;s the actual code now for the plugin:</p>
<pre class="brush: javascript">
/* jQuery.preloader - v0.95 - K Reeve aka BinaryKitten
*
* v0.95
* 	# Note - keeping below v1 as really not sure that I consider it public usable.
* 	# But it saying that it does the job it was intended to do.
* 	Added Completion of loading callback.
* 	Main Reworking With Thanks to Remy Sharp of jQuery for Designers
*
*
* v0.9
* 	Fixed .toString being .toSteing
*
* v0.8
*		Fixed sheet.href being null error (was causing issues in FF3RC1)
*
* v0.7
*		Remade the preLoadImages from jQuery to DOM
*
* v0.6
* 		Fixed IE6 Compatability!
*		Moved from jQuery to DOM
*
* v0.5
* 		Shifted the additionalimages loader in the preLoadAllImages so it wasn&#039;t called multiple times
* 		Created secondary .preLoadImages to handle additionalimages and so it can be called on itself
*/

(function ($) {
	$.preLoadImages = function(imageList,callback) {
		var pic = [], i, total, loaded = 0;
		if (typeof imageList != &#039;undefined&#039;) {
			if ($.isArray(imageList)) {
				total = imageList.length; // used later
					for (i=0; i &lt; total; i++) {
						pic[i] = new Image();
						pic[i].onload = function() {
							loaded++; // should never hit a race condition due to JS&#039;s non-threaded nature
							if (loaded == total) {
								if ($.isFunction(callback)) {
									callback();
								}
							}
						};
						pic[i].src = imageList[i];
					}
			}
			else {
				pic[0] = new Image();
				pic[0].onload = function() {
					if ($.isFunction(callback)) {
						callback();
					}
				}
				pic[0].src = imageList;
			}
		}
		pic = undefined;
	};

	$.preLoadCSSImages = function(callback) {
		var pic = [], i, imageList = [], loaded = 0, total, regex = new RegExp(&quot;url\((.*)\)&quot;,&#039;i&#039;),spl;
		var cssSheets = document.styleSheets, path,myRules,Rule,match,txt,img,sheetIdx,ruleIdx;
		for (sheetIdx=0;sheetIdx &lt; cssSheets.length;sheetIdx++){
			var sheet = cssSheets[sheetIdx];
			if (typeof sheet.href == &#039;string&#039; &amp;amp;&amp;amp; sheet.href.length &gt; 0) {
				spl = sheet.href.split(&#039;/&#039;);spl.pop();path = spl.join(&#039;/&#039;)+&#039;/&#039;;
			}
			else {
				path = &#039;./&#039;;
			}
			myRules = sheet.cssRules ? sheet.cssRules : sheet.rules;
			for (ruleIdx=0;ruleIdx &lt; myRules.length;ruleIdx++) {
				Rule = myRules[ruleIdx];
				txt = Rule.cssText ? Rule.cssText : Rule.style.cssText;
				match = regex.exec(txt);
				if (match != null) {
					img = match[1].substring(1,match[1].indexOf(&#039;)&#039;,1));
					if (img.substring(0,4) == &#039;http&#039;) {
						imageList[imageList.length] = img;
					}
					else if ( match[1].substring(1,2) == &#039;/&#039;) {
						var p2 = path.split(&#039;/&#039;);p2.pop();p2.pop();p2x = p2.join(&quot;/&quot;);
						imageList[imageList.length] = p2x+img;
					}
					else {
						imageList[imageList.length] = path+img;
					}
				}
			};
		};

		total = imageList.length; // used later
		for (i=0; i &lt; total; i++) {
			pic[i] = new Image();
			pic[i].onload = function() {
				loaded++; // should never hit a race condition due to JS&#039;s non-threaded nature
				if (loaded == total) {
					if ($.isFunction(callback)) {
						callback();
					}
				}
			};
			pic[i].src = imageList[i];
		}

	};
	$.preLoadAllImages = function(imageList,callback) {
		if (typeof imageList != &#039;undefined&#039;) {
			if ($.isFunction(imageList)) {
				callback = imageList;
			}
			else if (!$.isArray(imageList)) {
				imageList = [imageList];
			}
		}
		$.preLoadCSSImages(function(){
			if (imageList.length &gt; 0) {
				$.preLoadImages(imageList,function(){
					callback();
				});
			}
			else {
				callback();
			}
		});
	}
})(jQuery);
</pre>
<p>So now with these in place we can call them as so:</p>
<pre class="brush: javascript">
     $.preLoadImages(
          [
               &#039;http://www.google.co.uk/intl/en_uk/images/logo.gif&#039;,
               &#039;http://l.yimg.com/eur.yimg.com/i/uk/hp/yahoo1.png&#039;,
               &#039;http://tk2.stc.s-msn.com/br/hp/11/en-us/css/i/msn_b2.gif&#039;
          ],function(){
               alert(&#039;All Passed Images Loaded&#039;);
          }
     );
</pre>
<pre class="brush: javascript">
     $.preLoadCSSImages(function(){
          alert(&#039;All CSS Images Loaded&#039;);
     });
</pre>
<pre class="brush: javascript">
     $.preLoadAllImages(
          [
               &#039;http://www.google.co.uk/intl/en_uk/images/logo.gif&#039;,
               &#039;http://l.yimg.com/eur.yimg.com/i/uk/hp/yahoo1.png&#039;,
               &#039;http://tk2.stc.s-msn.com/br/hp/11/en-us/css/i/msn_b2.gif&#039;
          ],function(){
               alert(&#039;All Passed Images and All CSS Images Loaded&#039;);
          }
     );
</pre>
<p>To match up with Robert&#8217;s request for a loader image&#8230;</p>
<pre class="brush: javascript">
$.preLoadImages(&#039;/images/loader.gif&#039;,function() {
  /* Pre Load the loader gif first */
  $(&#039;&lt;img /&gt;&#039;).attr({
  	src:&#039;/images/loader.gif&#039;,
  	id:&#039;loader&#039;
  }).appendTo(&#039;#position&#039;);
  /* now preload stuff */
  $.preLoadCSSImages(function() {
     $(&#039;#loader&#039;).remove();
  })
});
</pre>
<p>Hopefully you have found this useful, as usual.. comments gratefully received.</p>
<p>[edit]<br />
 Thanks to Roberto for pointing out that the source had become corrupted in the post </p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.me.uk/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html/feed</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
	</channel>
</rss>
