<?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>buyco.ca &#187; Web Dev Tips and Tricks</title>
	<atom:link href="http://www.buyco.ca/category/how-to/web-dev-tips-and-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.buyco.ca</link>
	<description>because someone stole my name</description>
	<lastBuildDate>Fri, 23 Jul 2010 21:35:30 +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>Leveraging JavaScript Frameworks to Make Your Life Easier</title>
		<link>http://www.buyco.ca/2009/08/28/leveraging-javascript-frameworks-to-make-your-life-easier/</link>
		<comments>http://www.buyco.ca/2009/08/28/leveraging-javascript-frameworks-to-make-your-life-easier/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 09:51:44 +0000</pubDate>
		<dc:creator>Buyco</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Dev Tips and Tricks]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.buyco.ca/?p=132</guid>
		<description><![CDATA[Are you all about programming in JavaScript? Save yourself some time and try implementing a scripting framework. ]]></description>
			<content:encoded><![CDATA[<h4>Framework Preface</h4>
<p>Are you a hardcore JavaScript/AJAX programmer who knows no bounds? Do you spend meticulous hours perfecting your code so it works on all browsers? I used to be like that. I still am, but the only difference is that I now don&#8217;t spend hours testing my code. This is mostly attributed to the Prototype framework. Today I&#8217;m going to go through a few features and benefits of using a JavaScript framework over writing custom code yourself which may require testing and re-testing. Of course there is still the need for testing even when using a framework, but the difference is that there will be a lot of cross-browser compliance which is no longer needed.
<p>There are other frameworks out there like jQuery and MooTools, but today we&#8217;ll start with what I&#8217;m more familiar with and with what I first started with. The documentation for Prototype is very thorough and I don&#8217;t want to explain everything, so I&#8217;ll start off with the major items which will change your JavaScript programming life!</p>
<h4>JavaScript Framework? Double you tee eff?</h4>
<p>You&#8217;re asking the right question. For the longest time I was always stuck in my own little JavaScript world where everything I coded was job-specific, customized, and with my own techniques. There were also little quirks which JavaScript had when rendering/running under different browsers/operating systems&#8211;these too were also solved using my own super secret techniques. What Prototype does, as well as other JavaScript frameworks, is give you the simple building-blocks which solve those quirks without you even needing to address them specifically. For example, if you want a <em>div</em> element to show or hide, you simply select the element and program the action. Sounds simple, but selecting that element requires some browser detection and compatibility verification. Let&#8217;s make a couple comparisons..</p>
<h4>Make your life easier</h4>
<p>Continuing on with the previous example, let&#8217;s select that <em>div</em> element given the following markup:</p>
<pre class="brush: xml">
&lt;div id="copy">
	&lt;p>Lorem ipsom dolor sit amet&lt;/p>
&lt;/div>
</pre>
<p>We&#8217;re not going to think of any invokers right now, let&#8217;s just try to hide the <em>div</em>. Here is what the JavaScript may have looked like prior to using Prototype:</p>
<pre class="brush: jscript;">
var objDivId = 'copy';
objDiv = document.layers ? document.layers[objDivId] : document.all ? document.all[objDivId] : document.getElementById ? document.getElementById(objDivId) : null;
objDiv != null ? objDiv.style.display = 'none' : '';
</pre>
<p>Obviously this isn&#8217;t the most efficient way of doing this, ideally I would create a function, but this is the shortest of shorthands to select and hide the <em>div</em> element while respecting all web browsers. Now let&#8217;s have a look at how this would look using the Prototype framework:</p>
<pre class="brush: jscript;">
var objDivId = 'copy';
objDiv = $(objDivId);
$(objDivId) ? $(objDivId).style.display = 'none' : '';
</pre>
<p>For this example, the difference is quite apparent. By using the $() utility method we can quickly select that <em>div</em> element without the worry of what browser is being used. Now, let&#8217;s see what happens when we try to hide more than one element. First here&#8217;s the HTML markup:</p>
<pre class="brush: xml">
&lt;div id="copy">
	&lt;p>Lorem ipsom dolor sit amet&lt;/p>
&lt;/div>
&lt;div id="button">
	&lt;p>This is a button&lt;/p>
&lt;/div>
&lt;div id="preview">
	&lt;p>This is a preview section&lt;/p>
&lt;/div>
</pre>
<p>I&#8217;m going to create a function to hopefully help the cause of the traditional JavaScript code and see how it compares to using Prototype. First the oldschool method:</p>
<pre class="brush: jscript;">
function getObject(id) {
	return document.layers ? document.layers[id] : document.all ? document.all[id] : document.getElementById ? document.getElementById(id) : null;
}
var objDivIds = new Array('copy','button','preview');
for (var i=0; i&lt;objDivIds.length; i++) {
	objDiv = getObject(objDivIds[i]);
	objDiv.style.display = 'none';
}
</pre>
<p>And now the code for doing the same thing using Prototype:</p>
<pre class="brush: jscript;">
$('copy','button','preview').invoke('hide');
</pre>
<p>Say what!? Yeah, it&#8217;s that easy. The traditional method utilizes a function which will return the object instance. We then iterate through the three <em>div</em> elements and update each one individually.</p>
<p>In the Prototype version, we&#8217;re using a couple of included extensions. First, the selector $() utility method returns an Enumerable list of objects, and the <em>hide</em> function is invoked across all of those objects. Easy fo-sheezy.</p>
<p>There are also other ways of selecting multiple elements within a page. You can do this by applying some CSS knowledge. For example, let&#8217;s attach a tracking script to all of the links within a page. We want to first select all of the <em>A</em> elements, then iterate through them by adding a short querystring tag to it&#8217;s <em>href</em> attribute.</p>
<pre class="brush: jscript;">
var _TAG = '?source=buyco.ca';
$$('a').each(function(objA) {
	var url = objA.getAttribute('href');
	objA.setAttribute('href',url+_TAG);
});
</pre>
<p>This time I&#8217;m using the $$() utility method which will select a collection of elements. It takes in CSS-style selectors, such as: &#8216;a.external&#8217; (select all a elements with the classname external), or &#8216;div#nav ul li&#8217; (select all list items in the div with the id of &#8216;nav&#8217;). The full documentation on this can be found on the <a href="http://www.prototypejs.org/api/utility" title="Prototype API" target="_blank">Prototype API documentation website</a>.</p>
<h4>Post face?</h4>
<p>These are two short examples of a few things you can do (quicker) by utilizing a JavaScript framework. You will spend less time focused on browser compliance and more on the functionality and interactivity of your websites. It&#8217;s not a quick transition, but practice does make perfect! The documentation on the <a href="http://www.prototypejs.org/" target="_blank" title="Prototype JS">Prototype website</a> is awesome, and I still find myself going back to learn new things. The main point I&#8217;m trying to get across here is that there are tools out there to make our lives (as web developers) a whole lot easier. Don&#8217;t get stuck in a place where you&#8217;re too comfortable or you&#8217;ll end up writing IE6-compliant code forever. Spend your time on more important things like user-experience, and wicked-awesome-flashy-effects. Your site visitors will thank you:)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.buyco.ca/2009/08/28/leveraging-javascript-frameworks-to-make-your-life-easier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Optimizing with Image Sprites and CSS for Beginners (Part 1)</title>
		<link>http://www.buyco.ca/2009/06/30/optimizing-with-image-sprites-and-css-for-beginners-part-1/</link>
		<comments>http://www.buyco.ca/2009/06/30/optimizing-with-image-sprites-and-css-for-beginners-part-1/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 00:20:59 +0000</pubDate>
		<dc:creator>Buyco</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web Dev Tips and Tricks]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[sprites]]></category>

		<guid isPermaLink="false">http://www.buyco.ca/?p=70</guid>
		<description><![CDATA[Image Sprites and fancy CSS code-work can save your website visitors' download time.
]]></description>
			<content:encoded><![CDATA[<h4>What the hell is an image sprite? Why do I care? How do I use it?!</h4>
<p>That&#8217;s probably the same thought process I had when I first started reading about image sprites. After a few minutes of investigation via Google, I wanted to try to implement this technique every and anywhere possible on my websites. This isn&#8217;t necessarily a required method of implementation of a web site but it is still something that every web developer should at least have an understanding of.</p>
<p>In this 2-part series I&#8217;ll go over what an image sprite is, when to use them, how to use them, as well as provide a few examples.</p>
<h4>What is an image sprite?</h4>
<p>Back in the olden days (circa 90s), multiple images were merged into a single graphic file to quickly composite 2D games. <a href="http://upload.wikimedia.org/wikipedia/commons/0/03/Sprite-example.gif" target="_blank">Here is an example</a> [source: wikipedia]. Certain parts of this single graphic would be extracted and used to animate objects, characters, etc. We can utilize the same methodology with our websites and optimize load times (as well as provide an easy means of updating graphics when the times come).</p>
<h4>How does an image sprite work in the web-world?</h4>
<p>One of the greatest things to happen to a web developer / designer is the invention of Cascading Style Sheets. We can use CSS to pick certain coordinates within a sprite and display those areas on our websites. It may sound a bit tedious to have to find the pixel by pixel locations of every sprite you want to use, but in doing this you may be able to shave off precious load times of your web page.</p>
<h4>Decreasing load times? Wtf?</h4>
<p>When you want to increase the performance of a website, one of your key objectives is to reduce the amount of time it takes for a particular page (or pages) to load. One way is to keep the number of HTTP requests to a minimum. Any time you insert an image, or include a JavaScript file, or any other file for that matter, it is an additional HTTP request. In the nature of a sprite, you are making a single HTTP request for that graphic, then referencing different parts of it to be displayed on a web page. This works great granted that the size of the single graphic is less than the size of all it&#8217;s containing graphics individually (we&#8217;re talking bits and bytes here, not lengths and widths).</p>
<p>Another potential advantage is browser caching. Take, for example, this navigation bar:</p>
<p><a href="http://www.buyco.ca/wp-content/uploads/2009/06/an-nav.jpg"><img class="alignnone size-full wp-image-71" title="an-nav" src="http://www.buyco.ca/wp-content/uploads/2009/06/an-nav.jpg" alt="an-nav" width="610" height="33" /></a></p>
<p>In this case we assume that this navigation bar is split up into 8 images: 4 for the &#8220;default state&#8221;, and 4 for the &#8220;hover state&#8221;. When the user hovers their mouse over a particular image, the &#8220;hover state&#8221; image is loaded on the fly. That&#8217;s a single HTTP request for that extra image. If a user does not hover over all the graphics, not all of them will load, so basically this page will only load the additional images if it *really* needs to. But still not the best situation&#8230;</p>
<p>Each graphic is around 1.097Kb &#8211; 0.997Kb totaling 8.11Kb. In actuality, I used a single sprite totaling 3.292Kb. By loading even only the &#8220;default state&#8221; images, we are still reaching a size larger than the single sprite.</p>
<h4>Applications</h4>
<p>That is only one situation where you can really utilize the benefits of using a sprite. Using them for navigation bars with hover states is probably the most common use, but some sites have taken it a step further.</p>
<p>Take Google for example. Shaving off a single millisecond of load time of their site can translate into some amazing results (probably because they get millions of hits a day). You may not see this type of traffic, but in either case you still don&#8217;t want to have a slow website.</p>
<p>Back to my point, lets have a look at a screenshot of my iGoogle homepage:</p>
<p><a href="http://www.buyco.ca/wp-content/uploads/2009/06/igoogle.gif"><img class="alignnone size-full wp-image-72" title="igoogle" src="http://www.buyco.ca/wp-content/uploads/2009/06/igoogle.gif" alt="igoogle" width="621" height="135" /></a></p>
<p>Looks like a simple logo right? In actuality, the logo is a part of an image sprite:</p>
<p><a href="http://www.buyco.ca/wp-content/uploads/2009/06/ico_sprite_classic.gif"><img class="alignnone size-full wp-image-73" title="ico_sprite_classic" src="http://www.buyco.ca/wp-content/uploads/2009/06/ico_sprite_classic.gif" alt="ico_sprite_classic" width="167" height="68" /></a></p>
<p>All those smaller icons are used as elements within the page. With some fancy CSS code-work, only a single image is used on this page! (That&#8217;s actually a lie since I have multiple gadgets running with external image sources).</p>
<p>Other high-volume sites like Yahoo and eBay also use image sprites so why don&#8217;t you!?</p>
<p>Tune in next time as I go through a few implementation examples: Optimizing with Image Sprites and CSS for Beginners (Part 2)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.buyco.ca/2009/06/30/optimizing-with-image-sprites-and-css-for-beginners-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
