<?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>Eli Kirk &#187; Blog</title>
	<atom:link href="http://elikirk.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://elikirk.com</link>
	<description></description>
	<lastBuildDate>Fri, 11 May 2012 21:29:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Design. Seeing it through to the end.</title>
		<link>http://elikirk.com/2012/05/11/design-seeing-it-through-to-the-end/</link>
		<comments>http://elikirk.com/2012/05/11/design-seeing-it-through-to-the-end/#comments</comments>
		<pubDate>Fri, 11 May 2012 21:29:48 +0000</pubDate>
		<dc:creator>Matt Millette</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=1034</guid>
		<description><![CDATA[The finishing touches are so important to have a successful creation. The proverbial &#8220;cherry on top.&#8221; If you&#8217;ve had the chance to make it to Marvel&#8217;s The Avengers you can see this principle in action. After two hours of great cinematized comic book action, good humor and off the charts CG work, the creators could [...]]]></description>
			<content:encoded><![CDATA[<h3>The finishing touches are so important to have a successful creation.</h3>
<p>The proverbial &#8220;cherry on top.&#8221; If you&#8217;ve had the chance to make it to Marvel&#8217;s The Avengers you can see this principle in action. After two hours of great cinematized comic book action, good humor and off the charts CG work, the creators could have pulled the ol&#8217; jump-to-black-and-roll-the-credits. Basically telling the audience that it&#8217;s over now, so beat it. Which could have been fine. I mean, the movie was great, so that&#8217;s good enough. Not in this case though. Marvel hired a great movie title agency, Method Design, to make sure faithful fans were enjoying every last second of the movie.</p>
<p>After given the task, Method Design creative director, Stephen Viola and his team set out to make an effectively memorable and impactful main-on-end title sequence to escort your imagination into the ho-hum credit roll. Viola says that the team went through over half a dozen concepts before landing on the final piece. Just a side note. This is a common occurrence that should happen if the best possible results are desired. That&#8217;s not to say that your first idea isn&#8217;t ever your best, but it&#8217;s good practice to explore a variety of ideas. For The Avengers in particular, they really wanted to take advantage of the chance to focus on the characters without… showing the characters.</p>
<blockquote><p>Viola says,<em>“They’re all very unique personalities and character traits and have unique powers. So it’s a story about group chemistry and how they are forced to work together. They wanted to really focus on that and the real stakes was a big theme for Joss.”</em> (Quote courtesy of <a href="http://www.fxguide.com/featured/vfx-roll-call-for-the-avengers/" rel="nofollow">fxguide.com</a>)</p></blockquote>
<p>So the challenge began. How do you show a message, thought or feeling without showing the message thought or feeling? The answer? Very carefully. Well, in the case of The Avengers, Viola&#8217;s team focused on the character&#8217;s damaged shells of armor and battered extensions of weaponry. All of which show heavy scaring after long fought battles with the enemy, each other and their own personal struggles.</p>
<p>&nbsp;</p>
<p><a href="http://elikirk.com/wp-content/uploads/2012/05/Screen-shot-2012-05-10-at-2.20.14-PM2.png" rel="prettyPhoto[g1034]"><img class="alignnone size-full wp-image-1057" src="http://elikirk.com/wp-content/uploads/2012/05/Screen-shot-2012-05-10-at-2.20.14-PM2.png" alt="" width="718" height="395" /></a></p>
<p>&nbsp;</p>
<p><em></em>To do this, Method Design takes the viewer on a macroscopic journey over, under, through and around these beaten metallurgic appendages like a person would tour some cavernous canyon valley in a helicopter. Utilizing lighting, varying depths of field, different rates of camera motion and other tricks of the trade we get to see all the minute details of the battle damage these heroes endured. Carbon encrusted blast holes, broken chain mail, scuffed and dented alloys, chipped paint and torn clothes. All of this coupled with a commitment to great typography. Viola&#8217;s team developed a custom typeface to compliment the Avengers logo. But, they still wanted to be true to the typography used in actual comics from back in the day. To achieve this they took the classic Bank Gothic and gave it a heroic alter ego so it compliments the typography in the Avengers logo. As the camera pauses momentarily from one of it&#8217;s detailed expeditions, we see the type as it materializes into focus at some strategic point along an arm contour or in a gaping hole in armor. Always paced and positioned so appropriately.</p>
<p>&nbsp;</p>
<p><img class="alignnone size-full wp-image-1059" src="http://elikirk.com/wp-content/uploads/2012/05/Screen-shot-2012-05-10-at-2.29.54-PM1.png" alt="" width="716" height="393" /></p>
<p>&nbsp;</p>
<p><a href="http://elikirk.com/wp-content/uploads/2012/05/sqnc21.png" rel="prettyPhoto[g1034]"><img class="alignnone size-full wp-image-1061" src="http://elikirk.com/wp-content/uploads/2012/05/sqnc21.png" alt="" width="716" height="395" /></a></p>
<p>&nbsp;</p>
<p>It really is something you have to see to fully appreciate. The combination of detailed visuals, great camera rigging techniques, well thought out typography, and an underlying concept that successfully unifies them all into a great experience. Whether it&#8217;s motion graphics, oil painting, writing a novel or even baking a cake, when all the details are carefully considered and you strive for the best possible end product, it will almost undoubtedly pay off and you will have a great feeling of satisfaction. Especially when others can see and appreciate the effort along with the creator.</p>
<p><em><a href="http://www.methodstudios.com/work/the-avengers">Watch the main-on-end sequence</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/05/11/design-seeing-it-through-to-the-end/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Backbone.js &#8212; A Beginner&#8217;s Tutorial to Events</title>
		<link>http://elikirk.com/2012/05/11/backbone-js-a-beginners-tutorial-to-events/</link>
		<comments>http://elikirk.com/2012/05/11/backbone-js-a-beginners-tutorial-to-events/#comments</comments>
		<pubDate>Fri, 11 May 2012 21:15:52 +0000</pubDate>
		<dc:creator>Chris Rock</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=972</guid>
		<description><![CDATA[Backbone.js is a javascript framework that helps you write structured, easy-to-manage javascript&#8211;giving an valuable tool to web developers who are writing complicated javascript applications. Although it&#8217;s been compared to using an MVC framework, it&#8217;s not quite the same&#8211;the view plays the role of controller as well&#8211;dealing with events. There&#8217;s a lot to learn about Backbone.js, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://documentcloud.github.com/backbone/">Backbone.js</a> is a javascript framework that helps you write structured, easy-to-manage javascript&#8211;giving an valuable tool to <strong>web developers</strong> who are writing complicated javascript applications. Although it&#8217;s been compared to using an MVC framework, it&#8217;s not quite the same&#8211;the view plays the role of controller as well&#8211;dealing with events. There&#8217;s a lot to learn about Backbone.js, but in this tutorial we&#8217;re going to concentrate on how the View handles Events.</p>
<h2>Demonstration &#8211; Executive Function Test</h2>
<p>Your executive function is your ability for your brain to switch between different tasks. We be building a simple test similar to what psychologists and neuroscientists use to measure executive function. We create two fields and make either a red square or blue square appear in one of them. If it&#8217;s a red square, the user clicks on the field the square is in. If it&#8217;s a blue square, they click on the opposite field. We need to track the number of attempts and the number of correct choices. We also need a way to start and restart the test. You can view the complete <a href="/demos/executive-function/" target="_blank">demonstration of the executive function test</a> here.</p>
<h2>The Model</h2>
<p>This example focuses on using the View, so the Model for this will be a simple repository for a handful of variables that we want easily accessible to the view.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Model <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">Model</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    defaults<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        score<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
        attempts<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
        currentLocation<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
        currentColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'red'</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>The HTML</h2>
<p>Backbone.js views have the power to dynamically create HTML and DOM elements using templates, but because we&#8217;re focusing on Events and we want to keep this example simple, we&#8217;ll just use some existing HTML. Here&#8217;s the relevant code.</p>
<div class="controls">
<ul><!-- These will be changed dynamically by the program --></p>
<li>Score: <span id="score">0</span></li>
<li>Attempts: <span id="attempts">0</span></li>
<li>Percentage: <span id="percentage">100%</span></li>
</ul>
<p><button id="start">Start Test</button> <button id="reset">Reset Test</button></div>
<h2>The View</h2>
<p>To start with, we create a View object, which we&#8217;ll call &#8220;Test&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Test <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    el<span style="color: #339933;">:</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    model<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">new</span> Model<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// See model above</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// .... future functions to go here .... //</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> test <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Test<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The view itself will be created when we create an instance of it at the end with <code>var test = new Test();</code>. We attach the view to the &#8216;body&#8217; tag which gives it access to the HTML that we created above. Then we create a new Model for it (an instance of the Model code we created above). Now, if we wanted information from the Model within the view&#8211;for example, the score&#8211;we simply use <code>this.model.get('score');</code>. We can set data by using something like <code>this.model.set('score', 2);</code>.</p>
<h2>Events</h2>
<p>Here&#8217;s an example of how the view in Backbone.js plays the role of the controller in a traditional MVC structure. We define events that can happen within the view here:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Test <span style="color: #339933;">=</span> Backbone.<span style="color: #660066;">View</span>.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// ... Setup code (see above) ... //</span>
&nbsp;
    events<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">'click button#start'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'start'</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">'click .field'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'score'</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">'click button#reset'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'reset'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #006600; font-style: italic;">// Event handling functions (details below)</span>
    start<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    score<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    reset<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// ... additional functions here ... //</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Each event describes the event, the selector to listen to, and the callback function. In the case of the first event, the <code>button</code> element with the <code>start</code> id is listening for a <code>click</code> event. When it gets that event, it runs the <code>start</code> function located in the view. This and all future function example are contained within the Test view, although that&#8217;s not expressly shown.</p>
<h3>Start Event</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">start<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addRandomSpriteToField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>The function is really simple&#8211;it just calls another function <img src='http://elikirk.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  The function it calls is a little more complicated. The inline comments explain what it does.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">addRandomSpriteToField<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Pick random field</span>
    <span style="color: #003366; font-weight: bold;">var</span> randomField <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Pick a random color</span>
    <span style="color: #003366; font-weight: bold;">var</span> randomColor <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    color <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>randomColor <span style="color: #339933;">==</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'red'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'blue'</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Pick a random location</span>
    <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.field'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.field'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> randomX <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> x<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> randomY <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> y<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Create a sprite with chosen attributes</span>
    <span style="color: #003366; font-weight: bold;">var</span> sprite <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'</span></pre></div></div>

<div class="sprite"></div>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">').css({
        '</span>background<span style="color: #339933;">-</span>color<span style="color: #3366CC;">' : color,
        '</span>left<span style="color: #3366CC;">' : randomX + '</span>px<span style="color: #3366CC;">',
        '</span>top<span style="color: #3366CC;">' : randomY + '</span>px<span style="color: #3366CC;">'
    });
    // Erase fields and add sprite to chosen field
    $('</span>.<span style="color: #660066;">field</span><span style="color: #3366CC;">').empty();
    $('</span>.<span style="color: #660066;">field</span><span style="color: #339933;">-</span><span style="color: #3366CC;">' + randomField).append(sprite);
    // Update model data
    this.model.set('</span>currentLocation<span style="color: #3366CC;">', randomField);
    this.model.set('</span>currentColor<span style="color: #3366CC;">', color);
}</span></pre></div></div>

<p>Notice that we&#8217;re using the jQuery/Zepto <code>$(selector)</code> functionality to manipulate the DOM. Backbone.js doesn&#8217;t include it natively so be sure to include it in your scripts.</p>
<h3>Click a Field Event</h3>
<p>The next event is bound to both the fields. Whenever they&#8217;re clicked they call the <code>score</code> function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">score<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Get the id of the clicked field</span>
    <span style="color: #003366; font-weight: bold;">var</span> fieldId <span style="color: #339933;">=</span> event.<span style="color: #660066;">target</span>.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>
        <span style="color: #009900;">&#40;</span> <span style="color: #006600; font-style: italic;">// Red square same field</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'currentLocation'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> fieldId <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'currentColor'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'red'</span>
        <span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
        <span style="color: #009900;">&#40;</span> <span style="color: #006600; font-style: italic;">// Blue square opposite field</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'currentLocation'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> fieldId <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'currentColor'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'blue'</span>
        <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'score'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'score'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'attempts'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'attempts'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">addRandomSpriteToField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setScore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>Because this event could be triggers from one of two different fields, we need to know which one was clicked. Here we use <code>event.target.getAttribute('data-id')</code>. Then we check to see if the user did the correct action&#8211;same field if it&#8217;s a red square, opposite field if it&#8217;s a blue square. If they did, we add the score to the model. Whether they got the right score or not, we add increase the number of attempts. Then we add a new sprite using the <code>addRandomSpriteToField</code> function we&#8217;ve already seen. Then we call the <code>setScore</code> function, which updates the DOM with the new score data:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">setScore<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#score'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'score'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#attempts'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'attempts'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'attempts'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// Avoid a divide by 0 error</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#percentage'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'score'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'attempts'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'%'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#percentage'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'100%'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<h3>Restart Event</h3>
<p>The next event is the restart, which is bound to the &#8220;Reset Test&#8221; button and calls the <code>reset</code> function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">reset<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Erase the fields</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.field'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Reset the data</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'score'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'attempts'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Reset DOM elements</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setScore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>At the end, we update the DOM with the <code>setScore</code> we&#8217;ve seen already.</p>
<p>So we can see how in Backbone.js, the Events and controlled and managed by the View. If we were using just jQuery to do this, it would be a mess of events bound to DOM elements that would have difficulty communicating with each other. Using Backbone.js, we were able to easily extract reused code into separate functions (<code>setScore</code> and <code>addRandomSpriteToField</code>). Using Backbone.js also allowed us to have an easily accessible source of data. All in all, using Backbone.js give the code better, cleaner structure and makes it easy to create and make future additions or edits.</p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/05/11/backbone-js-a-beginners-tutorial-to-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Store Form Data with HTML5 localStorage and jQuery</title>
		<link>http://elikirk.com/2012/05/01/store-form-data-html5-localstorage-jquery/</link>
		<comments>http://elikirk.com/2012/05/01/store-form-data-html5-localstorage-jquery/#comments</comments>
		<pubDate>Tue, 01 May 2012 19:57:32 +0000</pubDate>
		<dc:creator>Chris Coray</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[localStorage]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=945</guid>
		<description><![CDATA[Recently, Chris Rock wrote an article that gave a great summary on the ins and outs of localStorage. If you haven&#8217;t read it yet, you probably should as it will give you the basis for the following. This is a very simplistic jQuery function utilizing localStorage that I devised to help meet a specific need [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, <a href="http://elikirk.com/2012/04/25/local-web-storage-with-localstorage/">Chris Rock wrote an article</a> that gave a great summary on the ins and outs of localStorage. If you haven&#8217;t read it yet, you probably should as it will give you the basis for the following.</p>
<p>This is a very simplistic jQuery function utilizing localStorage that I devised to help meet a specific need for a local client here in Utah county. I have re-written it here to be more flexible. Allow me to present a scenario:</p>
<p>Let&#8217;s say you&#8217;re in the market for a new job. You&#8217;ve been to all of the major sites, uploaded your résumé multiple times, and finally you find a great company (like Eli Kirk) with what sounds like the perfect job. The application process is all online with a résumé uploader at the end. Fair enough. I&#8217;ve been in this exact spot, years ago, and the average online job application is usually multiple pages with many fields.</p>
<p>You read through the application, taking your time to phrase things just right, making sure to dot every &#8220;i&#8221; and cross every &#8220;t.&#8221; Finally, you get to the résumé uploader and go to put your file in their system. You hit the &#8220;submit&#8221; button and wait.</p>
<p>And wait.</p>
<p>And wait.</p>
<p>Suddenly, something happens! The screen changes! It&#8217;s an alert telling you that Internet Explorer/Firefox/Chrome/Safari has unexpectedly closed and needs to be restarted. After muttering (or screaming) some words you wouldn&#8217;t want your mother to hear you say, you re-start your browser program. If it&#8217;s an updated browser, there&#8217;s a good chance you&#8217;ll at least get the tab re-opened that had your online job application. However, when you go to look at the fields, you stare in horror at the freshly blank boxes, and realize that you now have to do the entire process again. The sound of your head banging against the desk can be heard a block away.</p>
<p>Now imagine the same scenario as above, but instead when the browser re-opens to your tab, all of your information is still hanging out, waiting for you to simply try and re-upload the résumé again. Instead of your head pounding the desk, your neighbors up to a block away hear a rousing version of the <a href="http://www.youtube.com/watch?v=S4BWhvIlFVE" rel="prettyPhoto""><em>Hallelujah Chorus</em></a>. Sounds pretty good, right?</p>
<p>If you&#8217;re the type of web developer that wants to provide your user with more &#8220;Hallelujah&#8221; and less <a href="http://www.youtube.com/watch?v=GzhH900EjgE" rel="prettyPhoto"">breaking of computers</a>, then this handy function can be easily implemented and tweaked to meet your needs. There is a working version of the code below at <a href="/demos/localstorage-form-demo" target="_blank">localStorage Form Demo</a>.</p>
<p>First, the entire code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>Storage<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.page-class form input, .page-class form textarea'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> getInputName <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> localStorage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span>getInputName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>content <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keyup focusout'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                localStorage<span style="color: #009900;">&#91;</span>getInputName<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The function is quite simple, but only deals with text inputs and textareas. We&#8217;ll address radio and checkbox inputs at the end.</p>
<p>First, we need to load the function after the fields have been written by the browser. This can be accomplished several ways, including putting the code in a JavaScript include in the footer, but since the site in question already had a general site-wide JavaScript file in the header, I put the function in there with a $(document).ready() call.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next, we need to test and see if the browser can handle localStorage. As <a href="http://elikirk.com/2012/04/25/local-web-storage-with-localstorage/" target="_blank">Chris Rock explained,</a> browsers that support localStorage include updated versions of Chrome, Firefox, Safari, and surprisingly, Internet Explorer 8+. For browsers that are not updated, we don&#8217;t want to run the risk of causing the entire site to stop functioning due to a JavaScript error.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>Storage<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In other words, if the browser supports the method &#8220;Storage,&#8221; execute the code in this statement.</p>
<p>When the page loads, we want to pre-fill the fields with data that the user may have saved on their local system. We don&#8217;t need this to run on every page on the website, so we&#8217;re going to give our page a class of &#8220;page-class&#8221; on the body tag and use that as our pre-set selector (WordPress assigns its own class to pages if you use <code>&lt;?php body_class(); ?&gt;</code> in the <code>&lt;body&gt;</code> element of your markup, but you can use whatever class you want so long as it matches the selector in your jQuery). Then we select the form specifically by name, although if your site has multiple forms and you want localStorage on all of them, you can make the selectors much more loose. The two major input methods for forms are input fields and textareas, so we&#8217;ll put in selectors to target both:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>Storage<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.page-class form input, .page-class form textarea'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>After we target the appropriate inputs and textareas, we need to pre-fill them. If you need a primer on how this works, again refer back to <a href="http://elikirk.com/2012/04/25/local-web-storage-with-localstorage/" target="_blank">Chris Rock&#8217;s article</a>. We re-use the input name as the name of the localStorage object, which makes it very easy to store and retrieve this data. If this is the first time loading the page, the user won&#8217;t have anything in localStorage. This can cause a JavaScript error, which may prevent the rest of the page from working properly, so we assign an &#8220;if&#8221; statement to first check and make sure that the localStorage is not null. Finally, we make the value of this input or textarea equal to the value stored in the localStorage item, thus pre-filling the field:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>Storage<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.page-class form input, .page-class form textarea'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> getInputName <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> localStorage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span>getInputName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>content <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now that we have established how to pre-load localStorage that has been stored in a previous session, we need to set the method for storing those values in the first place. This is very simple, again assigning the name of the input (now stored in the variable &#8220;getInputName&#8221;) as the name of the localStorage object. We assign the storage function to fire on &#8220;<a href="http://api.jquery.com/keyup/" target="_blank">keyup</a>&#8221; and &#8220;<a href="http://api.jquery.com/focusout/" target="_blank">focusout</a>&#8221; events in order to cover mobile devices such as Android tablets and iPads. Realistically, &#8220;focusout&#8221; alone should cover this, but in the event that the browser or computer crashes while typing in a long string of text in a textarea, the keyup backup is nice and should use a minimal of resources:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>Storage<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.page-class form input, .page-class form textarea'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> getInputName <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> localStorage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span>getInputName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>content <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keyup focusout'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                localStorage<span style="color: #009900;">&#91;</span>getInputName<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That&#8217;s it! That is the whole of the basic version of this function. Earlier, I mentioned that this version of the function does not address checkbox or radio inputs. For this article, I pieced together a script that does just that and included it on this page: <a href="/demos/localstorage-form-demo" target="_blank">Simple jQuery localStorage Demonstration</a>. If you understand the code above, then understanding how we are resolving this issue isn&#8217;t overly complex, it is just divided up slightly differently:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span><span style="color: #009900;">&#40;</span>Storage<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.page-class form input, .page-class form textarea'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> getInputName <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> content <span style="color: #339933;">=</span> localStorage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span>getInputName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=&quot;checkbox&quot;]'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">//if this is a checkbox...</span>
                <span style="color: #003366; font-weight: bold;">var</span> numberCheck <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">index</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">//...determine if this is checkbox 1, 2, 3, etc....</span>
                getInputName <span style="color: #339933;">=</span> getInputName<span style="color: #339933;">+</span>numberCheck<span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">//...then assign it a unique identifier based on its name + index</span>
                content <span style="color: #339933;">=</span> localStorage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span>getInputName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>content <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checked'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'checked'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #006600; font-style: italic;">//If there is a value in localStorage based on this identifier, check the box.</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checked'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'checked'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        localStorage<span style="color: #009900;">&#91;</span>getInputName<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #006600; font-style: italic;">//If checked, store</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        localStorage.<span style="color: #660066;">removeItem</span><span style="color: #009900;">&#40;</span>getInputName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #006600; font-style: italic;">//If it becomes unchecked, remove from storage</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=&quot;radio&quot;]'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">//If this is a radio input...</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>content <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[name=&quot;'</span><span style="color: #339933;">+</span>getInputName<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;]'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #006600; font-style: italic;">//...check each button...</span>
                        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> content<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #006600; font-style: italic;">//...and if the value of &quot;content&quot; (referenced above) matches...</span>
                            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checked'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'checked'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #006600; font-style: italic;">//...check this radio button.</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    localStorage<span style="color: #009900;">&#91;</span>getInputName<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #006600; font-style: italic;">//store the value of the button whenever one is clicked</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>content <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>content<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keyup focusout'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    localStorage<span style="color: #009900;">&#91;</span>getInputName<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Basically, we&#8217;re dividing up the meat of our function by input type. Inputs with radio and checkbox types will have multiple inputs with the same name. In the case of radio types, this isn&#8217;t a big problem—we simply find the input that matches the stored data and set it to being checked. With a checkbox, we need to create individual identifiers for each input with the same name (localStorage objects will store as strings, so arrays are more of a mess). The most simple way to do this is to find out how many inputs with the same name we have, figure out the index of this particular input, and assign that to the localStorage name for storage and retrieval. Also, storing data for the radio or checkbox inputs is easiest to do with the &#8220;onclick&#8221; event. Last, in the final &#8220;else&#8221; statement, we are doing the same storage/retrieval from the earlier version of the function.</p>
<p>If you have any questions or comments on this function, feel free to leave them in the comments section below! Also, check out <a href="/demos/localstorage-form-demo">localStorage Form Demo</a> to see these scripts in action!</p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/05/01/store-form-data-html5-localstorage-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Local Web Storage with localStorage</title>
		<link>http://elikirk.com/2012/04/25/local-web-storage-with-localstorage/</link>
		<comments>http://elikirk.com/2012/04/25/local-web-storage-with-localstorage/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 21:44:57 +0000</pubDate>
		<dc:creator>Chris Rock</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=938</guid>
		<description><![CDATA[All modern browsers (and Internet Explorer 8+) support the ability to store a small amount of data within the browser itself in what&#8217;s called Local Storage. The data stored here will persist between user sessions, and in this way it works a lot like cookies. But local storage is different from cookies in some big [...]]]></description>
			<content:encoded><![CDATA[<p>All modern browsers (and Internet Explorer 8+) support the ability to store a small amount of data within the browser itself in what&#8217;s called <strong>Local Storage</strong>.  The data stored here will persist between user sessions, and in this way it works a lot like cookies.  But local storage is different from cookies in some big ways.</p>
<ul>
<li>Data isn&#8217;t transferred with every page request like it is with cookies</li>
<li>Data can be stored in larger amounts (usually up to <strong>5MB</strong>)</li>
<li>Currently no way for user to prevent its usage</li>
</ul>
<h2>Local Storage Javascript API</h2>
<p>Local storage is accessible through the Javascript <code>window.localStorage</code> (or just <code>localStorage</code>) API. There are a number of functions available to manipulate the data in local storage.</p>
<ul>
<li><strong><code>localStorage.setItem('foo', 'value for foo')</code></strong> &#8211; Assigns data to a specific key.</li>
<li><strong><code>localStorage.getItem('foo')</code></strong> &#8211; Retrieves the data stored under the specified key. You can also retrieve it with the array style, like this: <code>localStorage['foo']</code>.</li>
<li><strong><code>localStorage.key(0)</code></strong> &#8211; Retrieves the name of the <em>key</em> that&#8217;s stored at the specified position (in this case 0).</li>
<li><strong><code>localStorage.length()</code></strong> &#8211; Returns the number of items stored in <code>localStorage</code>.</li>
<li><strong><code>localStorage.removeItem('foo')</code></strong> &#8211; Deletes the item stored with the specified key.</li>
<li><strong><code>localStorage.clear()</code></strong> &#8211; Clears all items stored in <code>localStorage</code>.</li>
</ul>
<h2>Storing Complex Data in Local Storage</h2>
<p>Local storage can only store simple strings. To store more complex data like arrays or objects, we first turn them into strings using <code>localStorage.setItem('foo', JSON.stringify(obj))</code> and when we retrieve them, we change them back with <code>JSON.parse(localStorage.getItem('foo'))</code>.</p>
<h2>Local Storage Demonstration</h2>
<p>To simply demonstrate how local storage could be used, I created a list of DVDs that a user could add to a &#8220;To Watch&#8221; list.  This list will persist even if the user closes their browser.  The list can be cleared by clicking a &#8220;Reset&#8221; button.</p>
<p><a href="/demos/dvdlist/" target="_blank">Local Storage Demonstration</a>.</p>
<p>First we needed a list of DVDs to choose from.  I used selections from my personal collection and put them into a simple unordered list.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;dvds&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2001: A Space Odyssey<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Across the Universe<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Amélie<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Cars<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Chungking Express<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- .... --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>If you never visited the site before, the &#8220;To Watch&#8221; section will be empty, waiting for you to fill it up.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;to-watch&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>To Watch<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Reset<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>First, we need to check to see if there&#8217;s already data stored in <code>localStorage</code>. If there is, we add them to the <code>toWatch</code> array, and to the &#8220;To Watch&#8221; list in the DOM.  This uses jQuery, so be sure to include it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> toWatch <span style="color: #339933;">=</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>localStorage<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'dvds'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// turn the stored string into an array</span>
    toWatch <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>localStorage<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'dvds'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Loop through the array and append the data to the &quot;To Watch&quot; list</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> toWatch.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.to-watch ul'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;li&gt;'</span> <span style="color: #339933;">+</span> toWatch<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/li&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now, when someone clicks on a DVD title, we need to add it to the &#8220;To Watch&#8221; list.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> toWatch <span style="color: #339933;">=</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.dvds li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Append to the existing array</span>
    toWatch.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Push the array into the local storage under the 'dvds' key. (This overwrites what's there)</span>
    localStorage.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dvds'</span><span style="color: #339933;">,</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>toWatch<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Append a copy of the 'li' element to the &quot;To Watch&quot; list</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.to-watch ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Finally, we need to clear the &#8220;To Watch&#8221; list when someone clicks the &#8220;Reset&#8221; button.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.to-watch button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    toWatch <span style="color: #339933;">=</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Empty the array</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.to-watch ul'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Empty the DOM list</span>
    localStorage.<span style="color: #660066;">removeItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dvds'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Clear the local storage</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Because we only have one item in our <code>localStorage</code> we could also do <code>localStorage.clear()</code>.</p>
<h2>Limitations of Local Storage</h2>
<p>Local Storage is great for user settings and preferences, but it has its limitations.</p>
<ul>
<li>The storage amount is limited (again, about <strong>5MB</strong>)</li>
<li>You can only store strings (yes, we can get around that, but it&#8217;s inconvenient)</li>
<li>You can&#8217;t retrieve data as flexibly as you can with something like SQL, e.g., <code>SELECT * FROM dvds WHERE rating = 'PG-13' ORDER BY title</code></li>
</ul>
<p>For more data-intensive applications, consider checking out something like <strong>Web SQL</strong> or <strong>IndexedDB</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/04/25/local-web-storage-with-localstorage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallax Effect with CSS and Javascript</title>
		<link>http://elikirk.com/2012/04/23/parallax-effect-with-css-and-javascript/</link>
		<comments>http://elikirk.com/2012/04/23/parallax-effect-with-css-and-javascript/#comments</comments>
		<pubDate>Mon, 23 Apr 2012 21:01:17 +0000</pubDate>
		<dc:creator>Chris Rock</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=926</guid>
		<description><![CDATA[Parallax is the phenomenon in which objects that are further away appear to move slower than objects that are closer. This can give an interesting 3D effect to websites. Our client Liqupel makes a waterproof nano-coating for mobile devices. While we were developing the rest of the site, they wanted to create a splash page [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Parallax</strong> is the phenomenon in which objects that are further away appear to move slower than objects that are closer.  This can give an interesting 3D effect to websites.  Our client <a href="http://liquipel.com">Liqupel</a> makes a waterproof nano-coating for mobile devices.  While we were developing the rest of the site, they wanted to create a splash page (get it?) to drive traffic to and create buzz around. The designers wanted a cool 3D effect and we were able to get it through using some fairly simple <strong>Javascript</strong> and <strong>CSS</strong></p>
<h2>The Parallax Effect</h2>
<p>First, let me show off how it turned out. The splash page served its purpose and is no longer &#8220;live&#8221; but we put up a demonstration of what it was like on our site. <a href="/demos/liquipel" target="_blank">Liquipel Parallax Demonstration</a>.  Notice as you move your mouse, the background moves too.  Also notice that there are distinct layers that move at different speeds.</p>
<h2>Layer Creation</h2>
<p>To create this effect, there needs to be different layers.  We simply used one large png file for each layer. You can view the individual layers here:</p>
<ul>
<li><a href="/demos/liquipel/images/water-layer-1.png" target="_blank">Parallax layer 1</a></li>
<li><a href="/demos/liquipel/images/water-layer-2.png" target="_blank">Parallax layer 2</a></li>
<li><a href="/demos/liquipel/images/water-layer-3.png" target="_blank">Parallax layer 3</a></li>
<li><a href="/demos/liquipel/images/water-layer-4.png" target="_blank">Parallax layer 4</a></li>
</ul>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;parallax&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;water water-layer4&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;water water-layer3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;water water-layer2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;water water-layer1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.parallax</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1090px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.water</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1090px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.water-layer1</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>../images/water-layer-<span style="color: #cc66cc;">1</span>.png<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.water-layer2</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>../images/water-layer-<span style="color: #cc66cc;">2</span>.png<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.water-layer3</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>../images/water-layer-<span style="color: #cc66cc;">3</span>.png<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.water-layer4</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>../images/water-layer-<span style="color: #cc66cc;">4</span>.png<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>We put those layers in an absolutely positioned div that was set to the height of the images and 100% width. Inside this div was four other divs, also set to the height of the images and 100% width.  The background of each of these divs are the individual images, each set to <code>background-position: top center</code>.  This keeps them centered on the page, no matter how wide the browser is.</p>
<h2>The Javascript</h2>
<p>This requires jQuery, so be sure to include it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> currentX <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> movementConstant <span style="color: #339933;">=</span> .015<span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mousemove</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>currentX <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> currentX <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageX</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> xdiff <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageX</span> <span style="color: #339933;">-</span> currentX<span style="color: #339933;">;</span>
    currentX <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageX</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.parallax div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> el<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> movement <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>xdiff <span style="color: #339933;">*</span> movementConstant<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> newX <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">position</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">left</span> <span style="color: #339933;">+</span> movement<span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'left'</span><span style="color: #339933;">,</span> newX <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In this implementation, we&#8217;re only changing the image based on the x position of the mouse, although I&#8217;ve seen implementations that use both the x and the y position. First, we need a global variable to store the last mouse position.  Then we use an event handler to listen for mouse movement.  This will compare the last position with the new position and get the difference.  With that difference, we loop through each div, preform the movement calculation and apply it to that div.</p>
<h2>Movement Calculation</h2>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> movement <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>xdiff <span style="color: #339933;">*</span> movementConstant<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is were we really calculate the parallax effect. We loop through each div using the <code>jQuery.each()</code> function, which allows us to assign an iterative key (<code>i</code>) to each element (<code>el</code>). Because we&#8217;ll multiply the movement by each iterative key, each layer will move a different amount.  The second layer will move twice as much as the first, the third will move three times, etc.  If you want to bottom layer to remain stationary, you could change it to <code>i * (xdiff * movementConstant)</code>. Becuase the interative key starts at zero, the bottom layer won&#8217;t move at all.</p>
<p>The amount the layers move is determined by the <code>movementConstant</code> variable.  If it&#8217;s one, the bottom layer will move as much as the mouse does, the next layer twice as much, etc.  This effect is way to much, so we toned it down, trying different numbers until we came to .015, which seemed to work perfectly for the effect we were looking for.</p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/04/23/parallax-effect-with-css-and-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP AJAX Long-polling Comet Demonstration</title>
		<link>http://elikirk.com/2012/04/17/php-ajax-long-polling-comet-demonstration/</link>
		<comments>http://elikirk.com/2012/04/17/php-ajax-long-polling-comet-demonstration/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 21:47:19 +0000</pubDate>
		<dc:creator>Chris Rock</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=904</guid>
		<description><![CDATA[One thing missing from web applications that is found in, say native applications, is the ability to &#8220;push&#8221; data or events to the application. HTTP was built on the idea that once you got the document, you were done. Clever developers have found a couple of ways to get around this: Web Sockets &#8211; Advanced [...]]]></description>
			<content:encoded><![CDATA[<p>One thing missing from web applications that is found in, say native applications, is the ability to &#8220;push&#8221; data or events to the application. HTTP was built on the idea that once you got the document, you were done. Clever developers have found a couple of ways to get around this:</p>
<ul>
<li><strong>Web Sockets</strong> &#8211; Advanced way for browsers to communicate with a web sockets server to send and receive data (supported only by newer browsers)</li>
<li><strong>Hidden iframe</strong> &#8211; use an iframe that references a file that never closes to which javascript snippets are written</li>
<li><strong>Short polling</strong> &#8211; ping the server every few seconds to see if there have been any changes</li>
<li><strong>Long polling</strong> &#8211; open a connection to the server and wait until there has been a change before ending the file, then starting again</li>
</ul>
<h2>Externally controlling a website</h2>
<p>I had wanted to create a page in which a user could use a mobile device to control a sprite on another page. I believe that future Web Apps will use functionality like this, especially as mobile devices become more ubiquitous.  After some research (and false starts with Web Sockets) I decided to use the <strong>long polling</strong> method to accomplish this.  To do that, we&#8217;ll need to make the following:</p>
<ul>
<li><strong>External Data</strong> &#8211; Storage accessible by both the controller and receiver</li>
<li><strong>Controller</strong> &#8211; A page that will send events to the external data storage</li>
<li><strong>Receiver</strong> &#8211; A page that will listen for changes to the external data storage</li>
</ul>
<p>Before we get into the details, let&#8217;s actually see it in action.  You&#8217;ll need to open the following links in two different browsers, windows, or devices. It&#8217;s especially impressive to open the controller in a mobile device and the receiver on a desktop browser.</p>
<ul>
<li><a href="/demos/sprite-mover/" target="_blank">The Receiver</a></li>
<li><a href="/demos/sprite-mover/controller.php" target="_blank">The Controller</a></li>
</ul>
<p>The receiver will have an image on it, and the controller with have a D-pad-like graphic. Click the d-pad up, down, left, or right and the graphic on the receiver should move. Very impressive, no? Let&#8217;s look into how this works. If you&#8217;d like, you can download the <a href="/demos/sprite-mover/sprite-mover.zip">source code</a>, which goes into more detail.</p>
<h2>The External Data</h2>
<p>You can use anything for this.  For simplicity we&#8217;ll just be using a flat text file called &#8220;move.txt&#8221;</p>
<h2>The Controller</h2>
<p>Let&#8217;s start with the easy part&#8211;sending directions to the external storage.  For simplicity, I&#8217;ve only included the relevant code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;control-pad&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;up&quot;</span> <span style="color: #000066;">data-direction</span>=<span style="color: #ff0000;">&quot;up&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;down&quot;</span> <span style="color: #000066;">data-direction</span>=<span style="color: #ff0000;">&quot;down&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">data-direction</span>=<span style="color: #ff0000;">&quot;left&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;right&quot;</span> <span style="color: #000066;">data-direction</span>=<span style="color: #ff0000;">&quot;right&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Here&#8217;s the javascript for controller.php. It uses jQuery, so be sure to include it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.control-pad a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> dir <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'data-direction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// Send direction</span>
        $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'controller-process.php'</span><span style="color: #339933;">,</span>
            data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'direction'</span> <span style="color: #339933;">:</span> dir <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next, we need to create the controller-process.php file for the controller to call with AJAX.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// External data source</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/move.txt'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Write direction to file</span>
<span style="color: #000088;">$fh</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cant open'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'direction'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is pretty simple&#8211;it writes the direction sent by the controller into the external data file.  This will change the timestamp on the file, which will signal the monitor that we have a new command to process.</p>
<h2>The Receiver</h2>
<p>This is the central file for the project.  It displays a sprite and provides javascript so that sprite can be controlled. It &#8220;listens&#8221; by doing an AJAX call to a script named move-monitor.php.</p>
<p>The only HTML needed in this file is just a div for the sprite itself.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;sprite&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Then there&#8217;s the javascript for moving the sprite around</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> moveSprite<span style="color: #009900;">&#40;</span>direction<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> step <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #3366CC;">'left'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.sprite'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'left'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'-='</span> <span style="color: #339933;">+</span> step<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #3366CC;">'up'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.sprite'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'top'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'-='</span> <span style="color: #339933;">+</span> step<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #3366CC;">'right'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.sprite'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'left'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'+='</span> <span style="color: #339933;">+</span> step<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>direction <span style="color: #339933;">==</span> <span style="color: #3366CC;">'down'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.sprite'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'top'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'+='</span> <span style="color: #339933;">+</span> step<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now for the important part&#8211;the AJAX call to the move-montior.php script.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> timestamp <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">function</span> listener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Fetch info from monitor. This file will not completely download until it changes</span>
    $<span style="color: #339933;">.</span>ajax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        type<span style="color: #339933;">:</span> <span style="color: #0000ff;">'GET'</span><span style="color: #339933;">,</span>
        url<span style="color: #339933;">:</span> <span style="color: #0000ff;">'move-monitor.php'</span><span style="color: #339933;">,</span>
        data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">'timestamp'</span> <span style="color: #339933;">:</span> timestamp <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        dataType<span style="color: #339933;">:</span> <span style="color: #0000ff;">'JSON'</span><span style="color: #339933;">,</span>
        async<span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        cache<span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
        success<span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            moveSprite<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'direction'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            timestamp <span style="color: #339933;">=</span> data<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            listener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Run listener again</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notice that this function get&#8217;s called again once the AJAX call is complete.  We&#8217;ll come back to that later. Also notice that <code>async</code> is set to <code>true</code> and <code>cache</code> is set to <code>false</code>&#8211;that&#8217;s important.</p>
<p>And then the move-monitor.php script itself.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// External data file: can use any source, DB etc.</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/move.txt'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$lasttime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$currenttime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Loop until the files timestamp changes</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$currenttime</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$lasttime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">usleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// slow it down a bit</span>
    <span style="color: #990000;">clearstatcache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$currenttime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Send json to receiver</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'direction'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'timestamp'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$currenttime</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is the real meat of what we&#8217;re trying to do, so let me explain it more specifically.  This file will loop until the timestamp on the data file is different than the timestamp sent by the controller. Because the file won&#8217;t be &#8220;finished&#8221; until this happens, the connection remains open until we get a new command to process. Once there&#8217;s a new command, the data is sent to the receiver and the process begins again.</p>
<p>That&#8217;s basically it.  This is a surprisingly fun little script to play with and it seems to impress people, especially when you&#8217;re controlling the website with your mobile device.</p>
<p>From here the next step might be to implement a way to have multiple people access the same page without overwriting each others moves&#8211;maybe each person could control their own sprite?  Be creative and have fun with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/04/17/php-ajax-long-polling-comet-demonstration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web App Basics for iOS Platforms</title>
		<link>http://elikirk.com/2012/04/16/web-app-basics-for-ios-platforms/</link>
		<comments>http://elikirk.com/2012/04/16/web-app-basics-for-ios-platforms/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 17:02:03 +0000</pubDate>
		<dc:creator>Chris Rock</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=899</guid>
		<description><![CDATA[Apple has provided a way for web app developers to make their web apps more like native iOS apps by giving the developers a way to create app icons and startup images.  Your website can be added as a &#8220;bookmark&#8221; by clicking on &#8220;Add to Home Page&#8221; from the Safari browser on your device.  If [...]]]></description>
			<content:encoded><![CDATA[<p>Apple has provided a way for web app developers to make their web apps more like native iOS apps by giving the developers a way to create app icons and startup images.  Your website can be added as a &#8220;bookmark&#8221; by clicking on &#8220;Add to Home Page&#8221; from the Safari browser on your device.  If you&#8217;ve configured everything properly, it will create an icon with your custom icon and when you start it up, it will display the startup image.</p>
<h2>Icons</h2>
<p>Currently, iOS icons come in four different sizes, depending on which device you&#8217;re using</p>
<ul>
<li><strong>iPhone/iPod Touch - </strong>57&#215;57</li>
<li><strong>iPhone/iPod Touch (retina display) &#8211; </strong>114&#215;114</li>
<li><strong>iPad &#8211; </strong>72&#215;72</li>
<li><strong>iPad (retina display) &#8211; </strong>144&#215;144</li>
</ul>
<p>You should specify all icon sizes in the &lt;head&gt; section of your html with code similar to the following.  Note that the rel= tag says &#8220;apple-touch-icon-precompsed&#8221;.  If you just put &#8220;apple-touch-icon&#8221; then iOS will automatically put a pretty shine graphic over the top of it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- iPhone --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-icon-precomposed&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">sizes</span>=<span style="color: #ff0000;">&quot;57x57&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-icon.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- iPhone (Retina) --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-icon-precomposed&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">sizes</span>=<span style="color: #ff0000;">&quot;114x114&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-icon-114x114.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- iPad --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-icon-precomposed&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">sizes</span>=<span style="color: #ff0000;">&quot;72x72&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-icon-72x72.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- iPad (Retina) --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-icon-precomposed&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">sizes</span>=<span style="color: #ff0000;">&quot;144x144&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-icon-144x144.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<h2>Startup Images</h2>
<p>When your web app starts, iOS needs to load Safari and then access the web site. While it&#8217;s doing this, it will display a startup image if you&#8217;ve specified one.  You need to specify one for each type of device, as well as the horizontal configuration for iPad devices.  They have the following dimensions:</p>
<ul>
<li><strong>iPhone/iPod Touch &#8211; </strong>320&#215;460</li>
<li><strong>iPhone/iPod Touch (retina) &#8211; </strong>640&#215;920</li>
<li><strong>iPad (vertical) -</strong>768&#215;1004</li>
<li><strong>iPad (horizontal) &#8211; </strong>748&#215;1024</li>
<li><strong>iPad (retina &#8211; vertical) - </strong>1536&#215;2008</li>
<li><strong>iPad (retina &#8211; horizontal) - </strong>1496&#215;2048</li>
</ul>
<p>The code to specify the startup images should also be put into your &lt;head&gt; section in your html. Notice that the standard iPhone one is last.  If you don&#8217;t put this one last, the startup image won&#8217;t work on older iPhones.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- iPad (Retina, portrait) --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">media</span>=<span style="color: #ff0000;">&quot;(device-width: 768px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image-1536x2008.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- iPad (Retina, landscape) --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">media</span>=<span style="color: #ff0000;">&quot;(device-width: 768px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image-1496x2048.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- iPad (portrait) --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">media</span>=<span style="color: #ff0000;">&quot;(device-width: 768px) and (orientation: portrait)&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image-768x1004.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- iPad (landscape) --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">media</span>=<span style="color: #ff0000;">&quot;(device-width: 768px) and (orientation: landscape)&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image-748x1024.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- iPhone (Retina) --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">media</span>=<span style="color: #ff0000;">&quot;(device-width: 320px) and (-webkit-device-pixel-ratio: 2)&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image-640x920.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- iPhone IMPORTANT: Make sure this one is last --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">media</span>=<span style="color: #ff0000;">&quot;(device-width: 320px)&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;apple-touch-startup-image-320x460.png&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p>While working on these and testing them, be sure to remember to refresh the web page when you make changes.  If you&#8217;ve already added the web app icon to the home screen, you&#8217;ll need to delete it and add it again if you make changes to the startup images or icons.</p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/04/16/web-app-basics-for-ios-platforms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Own Facebook Timeline in No Time.</title>
		<link>http://elikirk.com/2012/03/31/own-facebook-timeline-in-no-time/</link>
		<comments>http://elikirk.com/2012/03/31/own-facebook-timeline-in-no-time/#comments</comments>
		<pubDate>Sat, 31 Mar 2012 00:02:26 +0000</pubDate>
		<dc:creator>emily</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=861</guid>
		<description><![CDATA[Disclaimer: the following post is for true FB geeks, if you are not one and would like US to be your FB geek, stop reading now and call us.   In the land of social media, a new wave is rolling through this weekend as Facebook is moving all fan/group/business pages to the new Timeline [...]]]></description>
			<content:encoded><![CDATA[<p><em>Disclaimer: the following post is for true FB geeks, if you are not one and would like US to be your FB geek, stop reading now and call us.  </em></p>
<p>In the land of social media, a new wave is rolling through this weekend as Facebook is moving all fan/group/business pages to the new Timeline layout. The more you understand about the new features and capabilities, the stronger your brand and strategy can be!</p>
<p>While we thoroughly enjoyed the FB sponsored tour of the new layout, here are a few things that we at Eli Kirk are keeping in mind as we manage our clients’ social media:</p>
<p><em><strong>1.  Shout it from the Rooftops (or from the coverphoto)</strong></em></p>
<p>The new coverphoto is awesome because it allows you to communicate key messages to your target audience with high level visibility.  It is also a great opportunity to build the brand of your company.  Get creative with your approach and be strategic with your message.  Here are a few fun examples we enjoy:</p>
<p>- Craft your message like this <a href="http://coverphotobook.com/testimonials" target="_blank">one</a>:</p>
<p><a href="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.06.15-PM.png" rel="prettyPhoto[g861]"><img class="aligncenter size-medium wp-image-879" src="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.06.15-PM-300x152.png" alt="" width="300" height="152" /></a></p>
<p>- We love the use of color and the feeling of elegance on <a href="http://www.facebook.com/Tiffany" target="_blank">Tiffany &amp; Co&#8217;s FB</a>:</p>
<p><a href="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.04.28-PM.png" rel="prettyPhoto[g861]"><img class="alignleft size-medium wp-image-862" src="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.04.28-PM-300x176.png" alt="" width="300" height="176" /></a></p>
<p>- <a href="http://www.facebook.com/BYUID" target="_blank">BYU Idaho</a> held a cover photo contest to determine what their main image would be, a great way to engage your audience and have them invest in your brand.</p>
<p>- For those brands that can get away with a little extra humor, be creative with how treat the space!  <a href="http://imgur.com/mnO58" target="_blank">http://imgur.com/mnO58</a></p>
<p><a href="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.11.59-PM.png" rel="prettyPhoto[g861]"><img class="alignright size-medium wp-image-863" src="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.11.59-PM-300x139.png" alt="" width="300" height="139" /></a></p>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div>- We love the use of color here on <a href="http://www.facebook.com/gap" target="_blank">The Gap&#8217;s FB page</a></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div>- Say thank you like <a href="http://www.facebook.com/audi?__adt=2" target="_blank">Audi</a> does:</div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div><a href="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.39.39-PM.png" rel="prettyPhoto[g861]"><img class="aligncenter size-medium wp-image-880" src="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.39.39-PM-300x177.png" alt="" width="300" height="177" /></a></div>
<div></div>
<div></div>
<div></div>
<div>- Last of all, one of our very own favorite clients, <a href="http://www.facebook.com/RemedezHairspa" target="_blank">Remedez</a>:</div>
<div><a href="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.17.26-PM.png" rel="prettyPhoto[g861]"><img class="aligncenter size-medium wp-image-864" src="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.17.26-PM-300x176.png" alt="" width="300" height="176" /></a></div>
<div></div>
<div></div>
<div><em><strong>2. Own your Space</strong></em></div>
<p>The new layout doesn’t allow you to default to a landing page or a specific tab (a feature we will miss dearly).  And as they say, it’s all about “location, location, location.”  But with the new layout, what was previously known as tabs will now show up directly under the coverphoto.  However, only 2 of the additional pages will show at any given time (photos and location are required to show).  The rest can be seen by clicking the arrow.  So, be smart about what you are showing.</p>
<p>On the secondary pages, you can build in functionality for users to “like” the page prior to viewing the content.  That’s where your crazy cool creative apps should live along with special offers or “insider” content.</p>
<p>We liked:</p>
<p>-   <a href="http://www.facebook.com/BananaRepublic/app_240215386047664" target="_blank">Banana Republic</a> has a special campaign for families in the military.</p>
<p><em><strong> 3.  </strong><strong>Drive Traffic</strong></em></p>
<p>While we continue to mourn the ability to default to specific tabs, you can drive traffic to your secondary pages through outside campaigns and even through FB ads.</p>
<p>Simply use the URL of the secondary page as you would a landing page and embed that in your campaign materials.  You win because you can both increase your number of likes and focus on a directed message.</p>
<p>-   <a href="http://www.facebook.com/GUCCI/app_232208980182871" target="_blank">Gucci </a>does a great job of driving traffic based on interest and it links out to their website.</p>
<div><a href="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.35.30-PM1.png" rel="prettyPhoto[g861]"><img class="aligncenter size-medium wp-image-870" src="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.35.30-PM1-300x252.png" alt="" width="300" height="252" /></a></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div><em><strong>4.  Tell your Story</strong></em></div>
<p>Use the new timeline features to &#8220;repin&#8221; posts to the top of the page (hover over the right hand side and click on the pencil).  You can also &#8220;highlight&#8221; posts by clicking the star at the right hand corner and the post will span the page.  Be strategic and you can use your timeline to tell the story of your brand over time or by milestone and accomplishment (you can change/adjust dates on posts accordingly).</p>
<div><em><strong>5.  Go the Extra Mile</strong></em></div>
<p>Brand a unique iframe for your FB page to live within.  Use badges, recommendations and other of the advanced functionality to make Facebook truly work for you.  <a href="http://apps.facebook.com/audipulse/" target="_blank">Audi</a> is skilled at not only owning their brand on FB, but also driving traffic and keeping the conversation going&#8230; all with an Audi branded theme:</p>
<p><a href="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.40.17-PM.png" rel="prettyPhoto[g861]"><img class="aligncenter size-medium wp-image-871" src="http://elikirk.com/wp-content/uploads/2012/03/Screen-shot-2012-03-30-at-5.40.17-PM-300x169.png" alt="" width="300" height="169" /></a></p>
<p>&nbsp;</p>
<p><em><strong>If you are still reading at this point</strong></em>, then you must love social media as much as we do!  We&#8217;ve  asked around… why the move to Timeline?  Besides the common comment of “you get what you pay for and we aren&#8217;t paying for this&#8230;,” we’ve also heard that the Timeline layout makes it easier for users to find content relative to date.  It allows you to sort and highlight items quickly.  As a company, it also gives you prime real estate for branding and messaging, something the old layout was lacking.  Jury is still out for us, but we do like that they are changing up the game because it gives us new challenges and room for innovation!  Enjoy the move this weekend and message us your creative solutions!</p>
<p>If you need help with your coverphoto or developing a succint strategy for social media, call us!  We would love to brainstorm how we can help leverage your resources or manage it for you, so that you start to OWN Facebook for your company.</p>
<p>emily@elikirk.com, 801.377.9321 x.219</p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/03/31/own-facebook-timeline-in-no-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spam, Spam, WordPress, and Spam</title>
		<link>http://elikirk.com/2012/02/02/spam-wordpress-spam/</link>
		<comments>http://elikirk.com/2012/02/02/spam-wordpress-spam/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 17:06:05 +0000</pubDate>
		<dc:creator>Chris Coray</dc:creator>
				<category><![CDATA[Business Tip]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=786</guid>
		<description><![CDATA[We all hate spam. I don&#8217;t mean the mystery meat in a can that is a favorite in Hawaii and the subject of parodies from Monty Python and Weird Al Yankovic. I&#8217;m talking about the unwanted comments or e-mails brimming with suspicious intent, typically advertising for shady products. First of all, what exactly is spam [...]]]></description>
			<content:encoded><![CDATA[<p>We all hate <a href="http://en.wikipedia.org/wiki/Spam_(electronic)" target="_blank">spam</a>. I don&#8217;t mean the <a href="http://en.wikipedia.org/wiki/Spam" target="_blank">mystery meat in a can</a> that is a favorite in Hawaii and the subject of parodies from <a href="http://youtu.be/anwy2MPT5RE" target="_blank">Monty Python</a> and Weird Al Yankovic. I&#8217;m talking about the unwanted comments or e-mails brimming with suspicious intent, typically advertising for shady products.</p>
<p>First of all, what exactly is spam and how does it work? Unless you&#8217;re new to the Internet, you&#8217;re probably familiar with the basics of what spam is. How does it end up on your WordPress site, though?</p>
<p>In some rare instances, spam originates from individuals surfing the web and looking for sites that have sections for comments. When they find one, they fill out the information, answer the Captcha question if there is one, and then post their garbage comment. This is one of the hardest forms of spam to fight because it is done by an actual person who has logic, reasoning, and creativity. A person can easily bypass anti-spam measures, such as Captchas.</p>
<p>Most often, however, spamming is done by bots. Bots are scripted programs that are written with the intent of carrying out some function using an automated process. Bots are not all nefarious; some are used for security purposes. Spam bots are programmed to do the same thing as the human in the above scenario&mdash;cruise the internet looking for comment forms to fill out and then leave their garbage message&mdash;but since the bot is a program, it can do this at a much, much faster rate without the need for a bathroom break or food.</p>
<p>Part of the headache with anti-spam is that as the security measures get better or more elaborate, the people who write these programs develop more complex bots to get around the new security. When it is discovered that they can circumvent security, better security is put in place. When the bots encounter better security, their programmers make adjustments to get around <em>that</em> security&#8230;I think you get the idea. It is an endless loop that we have been in for years.</p>
<p>So what can you do? Luckily, there are ways of protecting your WordPress site that don&#8217;t involve a Masters Degree in Computer Science to keep the bots at bay.</p>
<p>A few weeks ago, I wrote about <a href="http://elikirk.com/2012/01/19/my-favorite-wordpress-plugins/">some of the plugins</a> that I, as a web developer, really enjoy for <a href="http://wordpress.org/" target="_blank">WordPress</a>. There was one plugin that I left off of the list on purpose. It was not a reflection of the plugin itself, but I had thought about this current topic as a follow-up and thought it more apropos here.</p>
<p><a href="http://wordpress.org/extend/plugins/akismet/" target="_blank">Akismet</a> is an anti-spam plugin that comes pre-installed with WordPress. In a nutshell, when a user comes to your site and fills out a comment form, the Akismet plugin takes the information supplied by the user and their Internet Service Provider (ISP) and checks that data against a spam database. If the user&#8217;s information has been flagged as spam, the plugin moves the comment to your spam folder automatically.</p>
<p>While this is some pretty nice functionality, it is not entirely necessary for the small business owner, and it is not fail proof. In truth, no method of deterring spammed comments <em>is</em> fail proof, but a combination of things and knowing what to look for when something does make it through can help. Where Akismet shines is the automated, hands-off approach. All a site owner needs to do is obtain a valid license (<a href="http://akismet.com/wordpress/" target="_blank">info at their site</a>), make sure the plugin is activated, and Aksimet does the rest.</p>
<p>Another alternative or backup is a plugin that adds Captchas to your comments section. There are many out there, so I won&#8217;t get into specific plugins, but if you would like to go that route, look for something that uses question-and-answer based security. Surprisingly enough, many bots are smart enough to pull an image, &#8220;read&#8221; the letters or numbers inside of it, and then answer the Captcha correctly, but many are still stumped by basic math problems or riddles with obvious answers like &#8220;What color was George Washington&#8217;s white horse?&#8221; On a side note, this can also act as a security gate against comment authors who don&#8217;t have the ability to respond intelligently. </p>
<p>&#8220;But Chris,&#8221; you may be saying to yourself, &#8220;What if I want more control over my comment moderation? What if I use Akismet and a captcha, and spam still makes it through?&#8221; Excellent questions! I&#8217;m very glad you asked, because I have a response.</p>
<table>
<tr>
<td style="padding-right:20px;"><a href="http://elikirk.com/wp-content/uploads/2012/02/discussion_ss.jpg" rel="prettyPhoto"><img src="http://elikirk.com/wp-content/uploads/2012/02/discussion_ss-150x150.jpg" alt="" title="discussion_screenshot" width="150" height="150" class="alignleft size-thumbnail wp-image-787" /></a></td>
<td>WordPress itself actually has some fairly decent spam protection built in. Inside the Dashboard, if you go to &#8220;Settings > Discussion&#8221; (click the thumbnail on the left for a screenshot), WordPress provides a number of options for getting rid of spam. Not all of the settings in this section are anti-spam, but let&#8217;s go through them quickly:</td>
</tr>
</table>
<p><strong>&#8220;Default article settings:&#8221;</strong> This section contains three options. The first notifies other blog users if you link to their site in your own blog post. The second allows other blogs to send you a notification when someone has linked to your site. Neither of these have much to do with spam, but typically its handy to enable both. The third allows people to comment on your blog posts inside your own site. This (and all of the settings in this section) can be turned on and off for individual blog posts.</p>
<p><strong>&#8220;Other comment settings:&#8221;</strong> The options here are fairly self-explanatory, but see the picture above for my recommendations on how to set these for the average blog. Most blogs want site viewers to be able to leave a comment without having to be actual site members, but requiring a name and e-mail helps to weed out some spam, because offenders can be traced more easily.</p>
<p><strong>&#8220;E-mail me whenever:&#8221;</strong> These settings send an e-mail to the addresses specified for the Administrator (&#8220;Settings > General&#8221;) and the blog author, when someone posts a comment or when a comment is ready to be moderated. To moderate a comment, go to the &#8220;Comments&#8221; section of the dashboard and mark the comments there as &#8220;Approved,&#8221; &#8220;Spam,&#8221; or &#8220;Trash.&#8221;</p>
<p>The next few sections are specifically used for anti-spam measurements, so we&#8217;ll talk about them in a little more detail.</p>
<p><strong>&#8220;Before a comment appears:&#8221;</strong> If you check the top option, &#8220;An administrator must always approve the comment,&#8221; you don&#8217;t need to check the bottom option, &#8220;Comment author must have a previously approved comment.&#8221; If the box is checked that the comment should always be approved, it will always need approval regardless of whether the comment author has a previously approved comment or not. The advantage to only checking the bottom box is that you only have to approve a comment from a legitimate source once, then WordPress remembers that person&#8217;s credentials (provided they use the same name and e-mail address) and will automatically approve any comments from that person in the future. This can get tricky with modern spamming techniques, but we&#8217;ll talk about how to spot spammed comments in a minute.</p>
<p><strong>&#8220;Comment Moderation:&#8221;</strong> One common reason for spam is that the offender is trying to post links to your blog. For them, it is free advertising, and provides trackback links that boost their ranks in search engines. It is the epitome of spam. Using the Comment Moderation settings, you can automatically flag a comment that contains even one link by putting a number in the box. If you have either setting in the last section, &#8220;Before a comment appears&#8221; checked, however, you don&#8217;t need to mess with this. If you would like to set it up, it is easy to use: if you want a comment flagged with just one link, put a &#8220;1&#8243; in the box. If you think you can tolerate one, but two links are too many, put a &#8220;2,&#8221; and so on. Sometimes, however, the link is hidden in the username or e-mail, which leads us to the next section.</p>
<p><strong>&#8220;When a comment contains any of these words</strong> in its content, name, URL, e-mail, or IP, it will be held in the moderation queue.&#8221; Again, if you have already specified that all comments require moderation, you don&#8217;t need this section. Otherwise, you can flag a comment for moderation if it contains text that matches anything in a list you put in this box. The IP, however, is a numerical value, so you can specify IPs using exact values or wildcards to be subject to moderation.</p>
<p>For an example of the first use, if I put the word &#8220;cheap&#8221; into this box, anytime a comment comes up with that word in it, it is automatically suspended for moderation. This is nice if you are worried about comments from legitimate sources getting trashed on accident. If a spammer comes along and comments about &#8220;cheap little blue pills,&#8221; the comment is flagged for moderation. Likewise, if a comment is made saying &#8220;I think the local government is cheap when it comes to education,&#8221; this might be a legitimate source so you don&#8217;t want to automatically banish it, which we&#8217;ll talk about in the next section. Instead, it just flags the comment for moderation. The second use is handy if you go to the Comments section of your blog and notice that a lot of garbage comments are coming from a specific IP address. IP Addresses are listed in the Comments Moderation panel on each comment. To ban by IP, you would copy the IP address into the text box, the same as a word. If you&#8217;re not entirely certain what that means, avoid blocking by IP and use content (word) flags instead.</p>
<p><strong>&#8220;Comment Blacklist:&#8221;</strong> By putting a list of words in this box, you tell your WordPress site that if a comment comes in containing certain words, it should automatically be marked as spam. You don&#8217;t care what the source is, you don&#8217;t want comments containing those words or terms on your site. &#8220;Naughty words&#8221; are perfect candidates for this list. It might make you feel dirty coming up with a list of these phrases on your own, but if you moderate all of your comments, it doesn&#8217;t take very long to figure out what words to flag.</p>
<p>Towards the bottom of this section is the <a href="http://en.support.wordpress.com/avatars/" target="_blank">&#8220;Avatars&#8221;</a> category, which has nothing to do with spam, so we&#8217;ll skip it (see the official <a href="http://en.support.wordpress.com/avatars/" target="_blank">WordPress Avatar Help page</a> for more information on those).</p>
<p>Great, you have your settings configured and you&#8217;re ready to repel the worst of spammers. You write intriguing blog posts like this one, search engines begin indexing your site. As the search engines make your site searchable, spam comments begin rolling in. Some comments getting past security might not look like spam right off. How do you tell the difference between the spam and the legitimate comments?</p>
<p>This is something that has become more and more difficult as spammers have written more sophisticated bots. Some are easy to spot by their name, e-mail address, or their over 150 links to &#8220;cheap appliances&#8221; (yes, this is a real-world example from our own site, I counted them). Others, however, use generic compliments to gain your trust. &#8220;This is exactly the information I was searching for!&#8221; &#8220;You are so right and you use your words so well!&#8221; &#8220;I will come back to your site for more information most certainly!&#8221; Others have information like &#8220;Tips for Success,&#8221; which sound intelligent but have nothing to do with post they are commenting on.</p>
<p>So why would spammers leave these seemingly innocuous comments?</p>
<p>Let&#8217;s go back to the box in our settings that said, &#8220;Comment author must have a previously approved comment.&#8221; By giving a compliment or otherwise gaining the site owner&#8217;s trust and getting their comment approved, they are free to go back and post their spam on your site. While I don&#8217;t advocate un-checking this box and moderating <em>all</em> comments, it is typically enough to watch for red flags before approving the comment in the first place. If the English used sounds like it was run through Google translate first (which it probably was), if it reads like a random bunch of English sentences that are unrelated to each other, or if the comment as a whole has nothing to do with the blog post, those are major flags. Some will even copy data out of your blog post into their comment to make you think its related, but if you read through the sentences created, 99.9 percent of the time it still doesn&#8217;t make sense. In the end, if you still aren&#8217;t sure, either copy and paste the e-mail address provided in the comment into a Google search to see if it has been flagged as a spam address, or simply don&#8217;t approve it. It is better to be safe than sorry.</p>
<p>As the average user might gather from this article, blocking spam is sometimes complicated and requires a little knowledge of what to watch for. Akismet and similar plugins can help remove a lot of the headache. Captchas are also sometimes useful, although more complicated bots have been programmed to bypass those as well. Even with security plugins, it is good to have a backup in place like comment moderation, should anything manage to slip through.</p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/02/02/spam-wordpress-spam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Now Hiring: Project Manager</title>
		<link>http://elikirk.com/2012/01/31/now-hiring-senior-project-manager/</link>
		<comments>http://elikirk.com/2012/01/31/now-hiring-senior-project-manager/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 02:31:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://elikirk.com/?p=774</guid>
		<description><![CDATA[We are seeking a project manager to oversee project management in a creative agency and Web development environment. Description The project manager will help guide and develop company culture relative to increasing the efficiency and profitability of all projects. He or she will: 1. Develop and refine project management processes and lead their implementation across [...]]]></description>
			<content:encoded><![CDATA[<p>We are seeking a project manager to oversee project management in a creative agency and Web<br />
development environment.</p>
<h2>Description</h2>
<p>The project manager will help guide and develop company culture relative to increasing the efficiency<br />
and profitability of all projects. He or she will:<br />
1. Develop and refine project management processes and lead their implementation across the agency;<br />
2. Directly manage Web development and other technical projects;<br />
3. Oversee a small team of account/project managers;<br />
4. Be the assigned account manager for Web design and development clients;<br />
5. Perform other account management responsibilities as assigned;<br />
6. Oversee quality assurance testing for all Web projects; and<br />
7. Fulfill other duties as assigned.</p>
<h2>Qualifications</h2>
<p>The ideal candidate will have:<br />
1. At least 4 years of dedicated project management experience;<br />
2. A professional certificate or academic degree/emphasis in project management or a related field;<br />
3. Demonstrated success in managing people and client relationships;<br />
4. Demonstrated mastery of project management processes and techniques;<br />
5. Mastery of project management software solutions, such as Basecamp; and<br />
6. Successful experience in managing Web development projects.</p>
<h2>Compensation</h2>
<p>Compensation will be negotiated commensurate with experience. Health insurance, including HSA, will be<br />
available following one full calendar month of employment. No relocation package is offered with this position.</p>
<h2>Application</h2>
<p>Submit a resume and cover letter to Bill Brady at <a href="mailto:bill@elikirk.com">bill@elikirk.com</a>. Employment and character references<br />
should also be included. Please direct all inquiries about the position to Bill Brady at 801-377-9321 x207.</p>
]]></content:encoded>
			<wfw:commentRss>http://elikirk.com/2012/01/31/now-hiring-senior-project-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

