<?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>Lost Resort Blog &#187; javascript preloading images</title>
	<atom:link href="http://www.lostresort.biz/blog/tag/javascript-preloading-images/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lostresort.biz/blog</link>
	<description>Talking about us and about web site development</description>
	<lastBuildDate>Thu, 08 Jul 2010 11:34:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Preloading images using Mootools JavaScript framework</title>
		<link>http://www.lostresort.biz/blog/2009/02/24/preloading-images-using-mootools-javascript-framework/</link>
		<comments>http://www.lostresort.biz/blog/2009/02/24/preloading-images-using-mootools-javascript-framework/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 08:41:10 +0000</pubDate>
		<dc:creator>Cristi_N</dc:creator>
				<category><![CDATA[Mootools Javascript Framework]]></category>
		<category><![CDATA[Asset Method:images]]></category>
		<category><![CDATA[javascript preloading images]]></category>
		<category><![CDATA[tweak web page presentation]]></category>

		<guid isPermaLink="false">http://www.lostresort.biz/blog/?p=328</guid>
		<description><![CDATA[When a visitor reaches a web page for the first time, and he or she has to watch how the images are fulfilling the placeholders floating the text around or not, could be turned into a &#8220;leave and forget this&#8221; experience. A neat aspect of your web page presentation can make your visitors to never [...]]]></description>
			<content:encoded><![CDATA[<p>When a visitor reaches a web page for the first time, and he or she has to watch how the images are fulfilling the placeholders floating the text around or not, could be turned into a <i>&#8220;leave and forget this&#8221;</i> experience. A neat aspect of your web page presentation can make your visitors to never come back. For many web pages a good tweak could be the option to preload the images using JavaScript and display them with the rest of the page content in one &#8220;shot&#8221; on visitor&#8217;s screen.</p>
<h3>Mootools JavaScript Framework</h3>
<p><a href="http://mootools.net/">Mootools JavaScript framework</a> is <cite title="http://mootools.net">designed for the intermediate to advanced JavaScript developer and allows you to write powerful, flexible, and cross-browser code with its elegant, well documented, and coherent API</cite>. I use this framework and so far I&#8217;m pleased with the way things are going. If you want to know more about please <a href="http://mootools.net/docs/">read the Mootools documentation</a>.<br />
In the next lines I will present how to use one of this framework plug-in, <a href="http://mootools.net/docs/Plugins/Assets#Assets:images">Assets Method:images</a> to preload images and the content after the operation is completed. It&#8217;s easy to understand and to reproduce.</p>
<h4>Assets Method:images</h4>
<p>What does this plug-in do? <cite title="http://mootools.net/docs/Plugins/Assets#Assets:images">Preloads an array of images (<em>as strings</em>) and returns an array of <code>img</code> elements.</cite>. The syntax is very simple:</p>
<blockquote><p>
<code>var myImages = new Asset.images(source[, options]);</code>
</p></blockquote>
<p>where <em>sources</em> &#8211; is an array or a string, of the <strong>paths of the image files</strong> and <em>options</em> &#8211; (object, optional) could be a function which is executed on <em>onProgress</em> or <em>onComplete</em> events for this plug-in.</p>
<h3>Optimizing web page appearance</h3>
<p>The purpose of using this plug-in is to have our page content displayed <b>after</b> the images within are loaded. Our page will have some text and let&#8217;s say 10 images with the size varying from 60Kb to 100Kb  to be displayed.<br />
First, we need to hide the content when web page is loaded. For doing this we have two options: one to set the <code>display</code> style property to <code>none</code>, and the second to set the <code>visibility</code> style property to <code>hidden</code>. The difference between this two options is that <code>display:none</code> means that the placeholder for the element is not present at all, and for <code>visibility:hidden</code> the placeholder exists on page but is not visible.</p>
<p>With Mootools JavaScript Framework we can set the desired style property using:</p>
<blockquote><p>
<code>$('myContent').setStyle('display', 'none');</code></p>
<p>&nbsp;<i>or</i></p>
<p><code>$('myContent').setStyle('visibility', 'hidden');</code>
</p></blockquote>
<p>The problem is that until the JavaScript library is loaded there is a slightly chance for our content to be shown until it has the style property set. So, it will be better to use a very trivial JavaScript function to set the style property which will be declared on <code><</code>head<code>></code> area, and call it inside the document body.</p>
<p>Our function will be as follows:</p>
<blockquote><p>
<code><br />
function hideContent{<br />
documentGetElementById('myContent').style.display = "none'<br />
}</code></p>
<p>&nbsp;<i>or</i><br />
<code><br />
function hideContent{<br />
documentGetElementById('myContent').style.visibility = "hidden';<br />
}</code>
</p></blockquote>
<p>It is important <em>where</em> the function should be fired up. So I place the JavaScript code just after the opening <code><</code>div<code>></code> tag with the id "myContent". Place the code before, and you will have a "null" object.<br />
At this point we have our content hidden. After this we can initiate the images preloading process. The <code>options</code> for the Asset Mootools Plug-in could be one of two functions which can be executed on onComplete or onProgress events. On onProgress we have a function which will create HTML elements so during the preloading process a "Loading content, please be patient ..." text string will be displayed, and on onComplete event a function will reveal the content of our page.</p>
<h4>Creating HTML elements</h4>
<p>One of the methods used to create HTML elements with Mootools is the <a href="http://mootools.net/docs/Element/Element#Element:constructor">Element constructor</a> which creates a new element of the type passed in. I chose to create two elements, one of division type and the second of image type, which will be injected after the text generated to be displayed. The image, which will be an animated GIF, is added just as a bit of flavor for our page presentation. The code will be executed based on MooTools' custom domready event.<br />
The elements will be created inside a division container. We have two options to place this container, before the main content container or after. If we place it at the end of page content, the string text will appear without the image, for a certain period of time, and this is an undesired effect, so it will be better to place it just after the opening <code><</code>body<code>></code> tag.</p>
<p>The JavaScript code will be as follows:</p>
<blockquote>
<p><code style="text-align:left">var myImages = new Asset.images(['images/img1.png', 'images/img2.png', 'images/img3.png', 'images/img4.png','images/img5.png', 'images/img6.png', 'images/img7.png', 'images/img8.png', 'images/img9.png', 'images/img10.png'], {<br />
&nbsp;&nbsp;onProgress: function(){<br />
			&nbsp;&nbsp;&nbsp;&nbsp;var myDisplayTextContainer = new Element('div', {id: 'loadingText'}); <i style="font-weight:400">//create text container</i><br />
			&nbsp;&nbsp;&nbsp;&nbsp;var myPreloadingImg = new Element('img', {id: 'loadingImg'}); <i style="font-weight:400">//create image</i><br />
                        &nbsp;&nbsp;&nbsp;&nbsp;myPreloadingImg.setProperty('src', 'images/preloading.gif'); <i style="font-weight:400">//set the image source</i><br />
			&nbsp;&nbsp;&nbsp;&nbsp;$('loading').grab(myDisplayTextContainer); <i style="font-weight:400">//grabs text string container</i><br />
			&nbsp;&nbsp;&nbsp;&nbsp;myPreloadingImg.inject(myDisplayTextContainer, 'after'); <i style="font-weight:400">//inject image after text string container</i><br />
			&nbsp;&nbsp;&nbsp;&nbsp;$('loadingText').set('text', 'Loading content, please be patient ...'); <i style="font-weight:400">//create text string</i><br />
                        &nbsp;&nbsp;},<br />
&nbsp;&nbsp;onComplete: function(){<br />
			&nbsp;&nbsp;&nbsp;&nbsp;$('loading').setStyle('display' , 'none'); <i style="font-weight:400">//or use $('loading').setStyle('visibility', 'hidden')</i><br />
			&nbsp;&nbsp;&nbsp;&nbsp;(function(){<br />
					$('myContent').setStyle('display', ''); <i style="font-weight:400">//or use $('myContent').setStyle('visibility', 'visible')</i><br />
			&nbsp;&nbsp;&nbsp;&nbsp;}).delay(250);<br />
			&nbsp;&nbsp;}<br />
})</code>
</p></blockquote>
<p>I use a delay time before the content is displayed, because the animated GIF creates an ugly visual effect just before is disappearing.<br />
Anyway, being a stochastic process, a different behavior is expected for each web browser, even the desired result may be as expected.<br />
Please follow the link, to see <a href="http://www.lostresort.biz/examples/javascript-preloading-images/mootools-asset-method-images.html" alt="see a demo here" title="See a demo here">how content is displayed using the Mootools Asset Method:images to preload images</a>.</p>
<h4>Limitations and issues using this method</h4>
<p>There could be a chance when using this method with <code>display</code> style property used to hide/show content, that the images will not be displayed on Internet Explorer 6. Using <code>visibility</code> style property instead to target this browser engine version will fix this issue. On the other hand, if the <code>visibility</code> property is used on Internet Explorer 7, there could be a chance that the images and some text will not appear on page, or they are shown after some time.<br />
Another issue is that, is more than one instance of the image injected after the text string container. This could simply be "cosmeticized" setting the <code>overflow</code> style property for the image container to <code>hidden</code> and setting the height of the container to a value that fits the text string and the preloading image heights.</p>
<h3>Preloading images using JavaScript</h3>
<p>If your are looking for some JavaScript functions to preload images, independent of any framework, <a href="http://www.webreference.com/programming/javascript/gr/column3/">here you can find a good example for a JavaScript Image Preloader</a>.</p>
<p>I hope this resource is useful.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lostresort.biz%2Fblog%2F2009%2F02%2F24%2Fpreloading-images-using-mootools-javascript-framework%2F&amp;linkname=Preloading%20images%20using%20Mootools%20JavaScript%20framework"><img src="http://www.lostresort.biz/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.lostresort.biz/blog/2009/02/24/preloading-images-using-mootools-javascript-framework/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
