<?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; How to</title>
	<atom:link href="http://www.buyco.ca/category/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.buyco.ca</link>
	<description>because someone stole my name</description>
	<lastBuildDate>Fri, 24 Jun 2011 01:23:42 +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 2)</title>
		<link>http://www.buyco.ca/2009/08/27/optimizing-with-image-sprites-and-css-for-beginners-part-2/</link>
		<comments>http://www.buyco.ca/2009/08/27/optimizing-with-image-sprites-and-css-for-beginners-part-2/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 09:34:02 +0000</pubDate>
		<dc:creator>Buyco</dc:creator>
				<category><![CDATA[Features]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[Tech]]></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=118</guid>
		<description><![CDATA[I know what a sprite is. Now what do I do?!]]></description>
			<content:encoded><![CDATA[<h4>I know what a sprite is, now what?</h4>
<p>In Part 1 of <a title="Optimizing with Image Sprites and CSS for Beginners" href="http://www.buyco.ca/2009/06/30/optimizing-with-image-sprites-and-css-for-beginners-part-1/">Optimizing with Image Sprites and CSS for Beginners</a> I went through an explanation of what sprites are, how they work, and why to even bother. This time around we&#8217;re going to jump into some implementation of these sprites. I am still calling this Beginner stuff (only so that I can hit everyone with some hardcore code in the future), but if you happen to get confused follow me on <a title="Follow Jonathan Buyco on Twitter" href="http://twitter.com/jonathanbuyco" target="_blank">Twitter</a> and ask away!</p>
<h4>What am I going to use the sprite for?</h4>
<p>First let&#8217;s figure out what we&#8217;re even going to use the sprite for. When you&#8217;re ready to start slicing up your design, use your intuition and think of the graphical elements which will be used and reused on your site; this usually includes your navigation and maybe some buttons. In this example I&#8217;m going to use a sprite to create a &#8220;close&#8221; button and a textual navigation bar (SEO FTW). Here&#8217;s what I&#8217;ll be working with:</p>
<p><img src="/images/sprites.gif" alt="sprites" /></p>
<h4>Let&#8217;s open with a close</h4>
<p>This is the close button which I want sprite-ized:</p>
<p><a id="close" title="Close this badboy" href="#">Close</a></p>
<p>Your processes may be different, but I always start off with the HTML markup. The reason is because I want it as clean as possible and easily accessible for our search crawler friends. With a few quick keystrokes and a little touch of magic, this is what my markup for the close button looks like:</p>
<pre class="brush: xml;">&lt;a href="/close" title="Close this badboy" id="close"&gt;Close&lt;/a&gt;</pre>
<p>That was easy! Now let&#8217;s fancy this up with a bit of CSS magic and add the graphical close icon:</p>
<pre class="brush: css;">a#close {
	display: block;
	height: 14px;
	line-height: 14px;
	font: 11px 'Verdana','Arial',sans-serif;
	padding-left: 20px;
	background: url(sprites.gif) -32px -100px no-repeat;
}
a#close:hover {
	background-position: -32px -114px;
}
a#close:active {
	background-position: -32px -128px;
}</pre>
<h4>Wait.. what?</h4>
<p>I skipped about 20 steps there, so let&#8217;s go through this line by line:</p>
<pre class="brush: css; first-line: 2">	display: block;</pre>
<p>We need to give our link element layout so the graphic doesn&#8217;t get cropped. Display as <em>block</em>, kablammo problem solved! Almost..</p>
<pre class="brush: css; first-line: 3">	height: 14px;</pre>
<p>The close button consists of a 14&#215;14 area on our sprite map. We need to set the <em>height</em> so we don&#8217;t get any cropping.</p>
<pre class="brush: css;first-line: 4">	line-height: 14px;
	font: 11px 'Verdana','Arial',sans-serif;</pre>
<p>This part is a bit arbitrary. I set the <em>line-height</em> just so the text is vertically positioned evenly, and the font is a tribute to <a title="Ikea says Goodbye to Futurea" href="http://www.idsgn.org/posts/ikea-says-goodbye-to-futura/" target="_blank">Ikea</a> (don&#8217;t hurt me <img src='http://www.buyco.ca/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ).</p>
<pre class="brush: css;first-line: 6">	padding-left: 20px;
	background: url(sprites.gif) -32px -100px no-repeat;</pre>
<p>The fun part! The <em>padding</em> is there so that the text for &#8220;close&#8221; is shifted to the right and not disturbing the space for the graphic. But where the hell did I get the -32px -100px from? It&#8217;s the sprite&#8217;s position! 32 pixels from the left, and 100 pixels from the top. An easy way of finding your graphic positions in your sprite map is to open up the file in Photoshop, use the rectangular marquee tool (M), then start a selection from the top left of the document and end at the topmost and leftmost of the graphic. Check your Info window (F8) and take note of the width and height of your selection&#8211; and there are your positioning coordinates!</p>
<pre class="brush: css;first-line: 9">a#close:hover {
	background-position: -32px -114px;
}
a#close:active {
	background-position: -32px -128px;
}</pre>
<p>These modifiers are just added to give a hover and click effect to the button. The only style changing here is the position of the background which then points to the coordinates of the hover and active states. Put this all together and you have yourself a close button!</p>
<p>I obviously geared the markup for this single example, not taking into account that you may want to reuse the code. With just a small change to select the class of the elements instead of styling based on ID, you can have multiple instances of this button:</p>
<pre class="brush: css;">a.close {
	display: block;
	height: 14px;
	line-height: 14px;
	font: 11px 'Verdana','Arial',sans-serif;
	padding-left: 20px;
	background: url(sprites.gif) -32px -100px no-repeat;
}
a.close:hover {
	background-position: -32px -114px;
}
a.close:active {
	background-position: -32px -128px;
}</pre>
<p>For you newbies: it&#8217;s the dot.</p>
<h4>Navigation Sprite&#8217;ing</h4>
<div id="navigation">
<ul>
<li><a href="#1"><span id="nav-first"> </span><span>Link 1</span></a></li>
<li class="splitter"></li>
<li><a href="#2"><span>Link 2</span></a></li>
<li class="splitter"></li>
<li><a href="#3"><span>Link 3</span></a></li>
<li class="splitter"></li>
<li><a href="#4"><span>Link 4</span></a></li>
<li class="splitter"></li>
<li><a href="#5"><span id="nav-last"> </span><span>Link 5</span></a></li>
</ul>
</div>
<p><br style="clear: both;" /></p>
<p>This may seem a bit more advanced, but I am using very similar techniques as the example above. Once again, I&#8217;ll start off with the markup:</p>
<pre class="brush: xml;">
&lt;div id="navigation"&gt;
	&lt;ul title="Jon's awesome navigation"&gt;
		&lt;li&gt;&lt;a href="link1" title="link 1"&gt;&lt;span id="nav-first"&gt;&lt;/span&gt;&lt;span&gt;Link 1&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
		&lt;li class="splitter"&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="link2" title="link 1"&gt;&lt;span&gt;Link 2&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
		&lt;li class="splitter"&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="link3" title="link 1"&gt;&lt;span&gt;Link 3&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
		&lt;li class="splitter"&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="link4" title="link 1"&gt;&lt;span&gt;Link 4&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
		&lt;li class="splitter"&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="link5" title="link 1"&gt;&lt;span id="nav-last"&gt;&lt;/span&gt;&lt;span&gt;Link 5&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
&lt;/div&gt;</pre>
<p>Wow that looks like some dirty code. Forgive me for the empty <em>span</em> tags, it&#8217;s getting late. I&#8217;m assuming everyone&#8217;s navigation markup looks pretty similar to this, so it shouldn&#8217;t be much of a stretch to update your style sheets. Speaking of which, here they are for this example:</p>
<pre class="brush: css;">div#navigation {
	font: 11px 'Verdana','Arial',sans-serif;
}
div#navigation ul {
	margin: 0;
	padding: 0;
	height: 50px;
	background: url(sprites.gif) 0 0 repeat-x;
	float: left;
	list-style-type: none;
}
div#navigation ul li {
	float: left;
	display: block;
	width: 50px;
}
div#navigation ul li.splitter {
	width: 2px;
	height: 50px;
	background: url(sprites.gif) -7px -100px no-repeat;
	float: left;
}
div#navigation ul li a {
	display: block;
	height: 50px;
	line-height: 50px;
	color: #606;
	text-decoration: none;
}
div#navigation ul li a:hover {
	background: url(sprites.gif) 0 -50px repeat-x;
	color: #404
}
div#navigation ul li a span	{
	display: block;
	padding: 0 5px;
}
div#navigation ul li a span#nav-first {
	display: block;
	position: absolute;
	padding: 0 !important;
	width: 6px;
	height: 50px;
	background: url(sprites.gif) 0 -100px no-repeat;
}
div#navigation ul li a:hover span#nav-first {
	background: url(sprites.gif) -16px -100px
}
div#navigation ul li a span#nav-last {
	display: block;
	position: absolute;
	margin: 0 0 0 44px;
	padding: 0 !important;
	width: 6px;
	height: 50px;
	background: url(sprites.gif) -10px -100px no-repeat;
}
div#navigation ul li a:hover span#nav-last {
	background: url(sprites.gif) -26px -100px;
}</pre>
<p>Quite a bit of styles. It&#8217;s okay though, you&#8217;ve obviously gone through the first example a million times already, so this should be a breeze.</p>
<p>Now, a lot of the styling is there just for aesthetics so we&#8217;ll skip all of that. Let&#8217;s just jump to the areas where we&#8217;re using sprites:</p>
<pre class="brush: css;first-line: 4">div#navigation ul {
	margin: 0;
	padding: 0;
	height: 50px;
	background: url(sprites.gif) 0 0 repeat-x;
	float: left;
	list-style-type: none;
}</pre>
<p>Here we are actually going to be repeating the sprite as the background in the x direction because I want this navigation bar to expand in the event of new sections. When creating a sprite with the intention of repeating it as a background, keep in mind that they can only be repeated in one direction: x or y. You cannot repeat a slice of a sprite in both directions, otherwise you may have other graphics in the sprite included in the repetition. Looking back at the original sprite, this background can be repeated in the x direction since there are no other slices to the left or right of that area.</p>
<pre class="brush: css;first-line: 30">div#navigation ul li a:hover {
	background: url(sprites.gif) 0 -50px repeat-x;
	color: #404
}</pre>
<p>Skipping all the positioning and font styles, I want to add a hover effect. Once again I&#8217;m utilizing a repeating background in the x-direction. The position is found using my marquee technique above.</p>
<pre class="brush: css;first-line: 17">div#navigation ul li.splitter {
	width: 2px;
	height: 50px;
	background: url(sprites.gif) -7px -100px no-repeat;
	float: left;
}</pre>
<p>I thought the navigation bar would look a little weird without having some kind of splitter between each option, so the styles above should take care of that. Here I am actually specifying the width because I do not want the background to repeat.</p>
<pre class="brush: css;first-line: 38">div#navigation ul li a span#nav-first {
	display: block;
	position: absolute;
	padding: 0 !important;
	width: 6px;
	height: 50px;
	background: url(sprites.gif) 0 -100px no-repeat;
}
div#navigation ul li a:hover span#nav-first {
	background: url(sprites.gif) -16px -100px
}
div#navigation ul li a span#nav-last {
	display: block;
	position: absolute;
	margin: 0 0 0 44px;
	padding: 0 !important;
	width: 6px;
	height: 50px;
	background: url(sprites.gif) -10px -100px no-repeat;
}
div#navigation ul li a:hover span#nav-last {
	background: url(sprites.gif) -26px -100px;
}</pre>
<p>Here I&#8217;m just being a little fancy and adding the smooth bits at each end of the navigation bar. Essentially the same techniques used above.</p>
<h4>Holy Crap!</h4>
<p>Yeah, it&#8217;s a lot to take in but I didn&#8217;t lie when I said this was a beginner guide because I assume everyone reading this is an expert with CSS.</p>
<p>The examples above are merely examples. I hope no one copy-and-pastes the markup because it won&#8217;t work for your site (figuratively speaking). I hope you learn from what was presented above, test out your own techniques, and apply it in your daily web development ventures. The best way of learning is to do it yourself, this guide is only here as a guide and also because I have nothing better to do at 2:00AM on a Wednesday morning <img src='http://www.buyco.ca/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.buyco.ca/2009/08/27/optimizing-with-image-sprites-and-css-for-beginners-part-2/feed/</wfw:commentRss>
		<slash:comments>1</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>
		<item>
		<title>Certificate Authorities and their intermediate certificates</title>
		<link>http://www.buyco.ca/2008/06/28/ca-intermediate-certs/</link>
		<comments>http://www.buyco.ca/2008/06/28/ca-intermediate-certs/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 18:11:59 +0000</pubDate>
		<dc:creator>cturra</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[certificate]]></category>
		<category><![CDATA[intermediate]]></category>

		<guid isPermaLink="false">http://www.buyco.ca/?p=31</guid>
		<description><![CDATA[In this article I am going to pick on GoDaddy, but I want to be clear that they're not the only offender, VeriSign, arguably the largest CA, has the same problem!]]></description>
			<content:encoded><![CDATA[<p>I am going to assume if you&#8217;re reading this you know what the job of Certificate Authority (CA) is. If not, give the following a read: <a href="http://en.wikipedia.org/wiki/Certificate_authority">http://en.wikipedia.org/wiki/Certificate_authority</a>.</p>
<p>In this article I am going to pick on <a href="http://www.godaddy.com">GoDaddy</a>, but I want to be clear that they&#8217;re not the only offender, <a href="http://www.verisign.com">VeriSign</a>, arguably the largest CA, has the same problem!</p>
<p><strong>Warning</strong>: many reference to the word &#8220;certificate&#8221; to follow!</p>
<p>Okay, lets first start by looking at the issuer contents of a certificate signed by GoDaddy:</p>
<pre># openssl x509 -in signed_cert.crt -issuer -noout
issuer= /C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc./
OU=http://certificates.godaddy.com/repository/
CN=Go Daddy Secure Certification Authority/serialNumber=07969287</pre>
<p>The problem with the above is not that it was signed incorrectly, but that it was signed by an intermediate certificate and not the CA&#8217;s root certificate. Now, in most cases simply adding the respective intermediate certificate to your web server config (see <a href="http://httpd.apache.org">apache</a> example below) will be sufficient for browsers to now &#8220;trust&#8221; this certificate, however, <a href="http://getfirefox.com">Firefox</a> and <a href="http://www.microsoft.com/windows/products/winfamily/ie/default.mspx">IE7</a> are notorious for not (more on that below).</p>
<pre>SSLCACertificateFile    /path/to/certs/gd_intermediate.crt</pre>
<p>In the case of Firefox (and sometimes IE7), the browser doesn&#8217;t always have a trust chain back to the root CA with the intermediate issuer and thus reports the certificate as &#8220;untrusted.&#8221; There are two options here: 1) you can import the CA&#8217;s intermediate certificate into each users browser, which obviously doesn&#8217;t scale. Or (2) create a pkcs7 certificate that includes the certificate chain back to the CA&#8217;s root certificate:</p>
<pre># openssl crl2pkcs7 –nocrl &lt;ssl_public_crt&gt; /
–certfile &lt;intermediate_crt&gt; -outform PEM –out &lt;new_pkcs7_crt&gt;</pre>
<p>Using method #2, will result in an updated certificate which will need to be assigned to your respective web service rather than the certificate returned from your CA  (apache example below):</p>
<pre>SSLCertificateFile    /path/to/certs/&lt;new_pkcs7_crt&gt;</pre>
<p>You should no longer have certificate trust issues. happy times <img src='http://www.buyco.ca/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.buyco.ca/2008/06/28/ca-intermediate-certs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unobtrusive Image Rollover</title>
		<link>http://www.buyco.ca/2008/06/19/unobtrusive-image-rollover/</link>
		<comments>http://www.buyco.ca/2008/06/19/unobtrusive-image-rollover/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 21:13:59 +0000</pubDate>
		<dc:creator>Buyco</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[addEventListener]]></category>
		<category><![CDATA[attachEvent]]></category>
		<category><![CDATA[hover]]></category>
		<category><![CDATA[mouseover]]></category>
		<category><![CDATA[unobtrusive]]></category>

		<guid isPermaLink="false">http://www.buyco.ca/?p=23</guid>
		<description><![CDATA[Learn how to add some spice to your site! Unobtrusive programming should be common practice, and what better way to start than here.]]></description>
			<content:encoded><![CDATA[<p>As web developers we spend so much time trying to make our sites cutting edge yet still functional with older browsers. It&#8217;s a curse! How great would it be to have a web browser standard that everyone could adhere to. Too bad life isn&#8217;t that simple. In today&#8217;s example we&#8217;re going to create a simple javascript image rollover. Yes, I know we&#8217;ve all done it before, but today we&#8217;ll do it right: unobtrusively.</p>
<p>There are many ways to do this, but in this example we&#8217;re just going to do a simple image replacement on mouseover. We&#8217;ll start off with the code for the image:</p>
<pre>&lt;img src="/images/how-to/javascript/button-inactive.gif" id="button" /&gt;</pre>
<p>For your SEO enthusiasts and W3C fiends, I know there are missing attributes. We&#8217;re going to go as minimalist as possible to simplify this process. One attribute to note of is the &#8220;id&#8221; which we&#8217;ll be using to select this particular image. This is very important so that we can select this image element based on it&#8217;s id (DOM FTW!).</p>
<p>Next we want to apply an event handler to that object so that when the user moves the cursor over it, the image will change into something pretty! Internet explorer uses a different type of event handler code than other browsers (because if it didn&#8217;t, this would be way too easy). So we will ensure we do a little browser detection:</p>
<pre>var isMoz;
window.addEventListener ? isMoz = true : isMoz = false;</pre>
<p>The window.addEventListener method is not used by Internet Explorer, so here we just single those browsers out. Next we want to create the code to attach a function to the mouseover (or onmouseover) event of the button. We haven&#8217;t created the image swapping functions yet, but keep in mind that we&#8217;ll call them imgOver and imgOut. That being said, lets create the event listeners and put them in a function called initMouseOver:</p>
<pre>function initMouseOver() {
 var objImage = document.getElementById('button');
 if (isMozilla) {
  objImage.addEventListener('mouseover', imgOver, false);
  objImage.addEventListener('mouseout', imgOut, false);
 }
 else {
  objImage.attachEvent('onmouseover', imgOver);
  objImage.attachEvent('onmouseout', imgOut);
 }
}</pre>
<p>Now you see why the image&#8217;s ID is important (remember: DOM FTW!). In the above snippet, when the function is called, event listeners are added to the image object. In addEventListener and attachEvent, the first value passed in is called the event type. For a mouse over event, these are valued at mouseover and onmouseover. The second value is the function to be associated with the event. In addEventListener the last value taken in is a boolean value which indicates whether or not to bind the event as it propogates to the image or as the event bubbles upwards from the image. This is usually set to false.</p>
<p>Next we need to create the functions to swap the images in and out as the object captures the events:</p>
<pre>function imgOver(e) {
 var eMoz = 'target';
 var eIE = 'srcElement';
 var objImgOver = new Image();
 objImgOver.src = '/images/how-to/javascript/button-active.gif';
 e[eMoz] ? e[eMoz].src = objImgOver.src : e[eIE].src = objImgOver.src;
}
function imgOut(e) {
 var eMoz = 'target';
 var eIE = 'srcElement';
 var objImgOut = new Image();
 objImgOut.src = '/images/how-to/javascript/button-inactive.gif';
 e[eMoz] ? e[eMoz].src = objImgOut.src : e[eIE].src = objImgOut.src;
}</pre>
<p>In both functions, we&#8217;re initializing the new image, then swapping it. This is not the most efficient way to do this since we&#8217;re recreating a new image object every time an event occurs. So let&#8217;s clean up all the code and put it into a separate javascript file. Here&#8217;s how it should look:</p>
<pre>var isMoz;
window.addEventListener ? isMoz = true : isMoz = false;
var eMoz = 'target';
var eIE = 'srcElement';
var objImgOver = new Image();
objImgOver.src = '/images/how-to/javascript/button-active.gif';
var objImgOut = new Image();
objImgOut.src = '/images/how-to/javascript/button-inactive.gif';

function initMouseOver() {
 var objImage = document.getElementById('button');
 if (isMozilla) {
  objImage.addEventListener('mouseover', imgOver, false);
  objImage.addEventListener('mouseout', imgOut, false);
 }
 else {
  objImage.attachEvent('onmouseover', imgOver);
  objImage.attachEvent('onmouseout', imgOut);
 }
}
function imgOver(e) {
 e[eMoz] ? e[eMoz].src = objImgOver.src : e[eIE].src = objImgOver.src;
}
function imgOut(e) {
 e[eMoz] ? e[eMoz].src = objImgOut.src : e[eIE].src = objImgOut.src;
}</pre>
<p>*Deep breath* still with me? That&#8217;s essentially it! All you need to do now is to include the javascript file in your HTML and ensure the initMouseOver() function is called when the page loads. Simply add onload=&#8221;" to your body tag and it should look a little something like this:</p>
<pre>&lt;body onload="javascript:initMouseOver();"&gt;</pre>
<p style="text-align: center;"><img id="button" class="aligncenter" src="/images/how-to/javascript/button-inactive.gif" alt="" /></p>
<p>That&#8217;s it! No wait&#8211;that&#8217;s only for one image. What if we need to set this up for multiple images? Well, glad I asked because there are several ways of accomplishing this. If you think you can take more of this, carry on to one of the next tutorials or download all the work files for this one:</p>
<blockquote><p><a title="Example: Unobtrusive Image Rollover" href="http://www.buyco.ca/downloads/how-to/javascript/example-unobtrusive-javascript-rollover.zip" target="_blank">Example: Unobtrusive Image Rollover</a></p></blockquote>
<p>Next step: multiple buttons, preloading, and optimization</p>
]]></content:encoded>
			<wfw:commentRss>http://www.buyco.ca/2008/06/19/unobtrusive-image-rollover/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

