<?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>SoftLayer Blog &#187; web development</title>
	<atom:link href="http://blog.softlayer.com/tag/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.softlayer.com</link>
	<description>A Behind the Scenes Look at the Best Hosting Provider in the World</description>
	<lastBuildDate>Fri, 24 May 2013 18:19:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Script Clip: HTML5 Audio Player with jQuery Controls</title>
		<link>http://blog.softlayer.com/2013/script-clip-html5-audio-player-with-jquery-controls/</link>
		<comments>http://blog.softlayer.com/2013/script-clip-html5-audio-player-with-jquery-controls/#comments</comments>
		<pubDate>Thu, 07 Mar 2013 21:05:19 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[audio player]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[creating]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10633</guid>
		<description><![CDATA[HTML5 and jQuery provide mind-blowing functionality. Projects that would have taken hours of development and hundreds of lines of code a few years ago can now be completed in about the time it&#8217;ll take you to read this paragraph. If you wanted to add your own audio player on a web page in the past, [...]]]></description>
			<content:encoded><![CDATA[<p>HTML5 and jQuery provide mind-blowing functionality. Projects that would have taken hours of development and hundreds of lines of code a few years ago can now be completed in about the time it&#8217;ll take you to read this paragraph. If you wanted to add your own audio player on a web page in the past, what would it have involved? Complicated elements? Flash (*shudders*)? It was so complicated that most developers just linked to the audio file, and the user just downloaded the file to play it locally. With HTML5, an embedded, cross-browser audio player can be added to a page with five lines of code, and if you want to get really fancy, you can easily use jQuery to add some custom controls.</p>
<p>If you&#8217;ve read any of my <a href="http://blog.softlayer.com/author/cwolff/">previous blogs</a>, you know that I love when I find little code snippets that make life as a web developer easier. My go-to tools in that pursuit are HTML5 and jQuery, so when I came across this audio player, I knew I had to share. There are some great jQuery plugins to play music files on a web page, but they can be major overkill for a simple application if you have to include comprehensive controls and themes. Sometimes you just want something simple without all of that overhead:  </p>
<p><audio style="width:550px; margin: 0 auto; display:block;" controls><br />
  <source src="http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.ogg" type="audio/ogg"><br />
  <source src="http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.mp3" type="audio/mpeg"><br />
Your browser does not support the audio element.<br />
</audio></p>
<p>Oooh&#8230; Ahhh&#8230;</p>
<p>That song &mdash; Pop Bounce by SoftLayer&#8217;s very own <a href="http://www.reverbnation.com/chrisinterrante">Chris Interrante</a> &mdash; is written in five simple lines of HTML5 code:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;audio <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:550px; margin: 0 auto; display:block;&quot;</span> controls&gt;</span>
  <span style="color: #009900;">&lt;source <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.ogg&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audio/ogg&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;source <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.mp3&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audio/mpeg&quot;</span>&gt;</span>
Your browser does not support the audio element.
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>audio&gt;</span></pre></div></div>

<p>If IE 9+, Chrome 6+, Firefox 3.6+, Safari 5+ and Opera 10+ would all agree on supported file formats for the <code>&lt;audio&gt;</code> tag, the code snippet would be even smaller. I completely geek out over it every time I look at it and remember the days of yore. As you can see, the HTML5 application has some simple default controls: Play, Pause, Scan to Time, etc. As a developers, I couldn&#8217;t help but look for a to spice it up a little &#8230; What if we want to fire an event when the user plays, pauses, stops or takes any other action with the audio file? jQuery!</p>
<p>Make sure your jQuery include is in the <code>&lt;head&gt;</code> of your page:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>Now let&#8217;s use jQuery to script separate &#8220;Play&#8221; and &#8220;Pause&#8221; links &#8230; And let&#8217;s have those links fire off an alert when they are pressed:</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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#play-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You have played the audio file!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>    
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#pause-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You have paused the audio file!'</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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>With that script in the <code>&lt;head&gt;</code> as well, the HTML on our page will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span>:<span style="color: #ff0000;">&quot;audioplayer&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;audio <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audioplayer&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audioplayer&quot;</span> controls loop&gt;</span>
    <span style="color: #009900;">&lt;source <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.ogg&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audio/ogg&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;source <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.mp3&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audio/mpeg&quot;</span>&gt;</span>
  Your browser does not support the audio element.
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>audio&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;play-button&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Play!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pause-button&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Pause!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Want proof that it works that simply? <a href="http://cdn.softlayer.com/innerlayer/audioSL.html">Boom.</a></p>
<p>You can theme it any way you like; you can add icons instead of the text &#8230; The world is your oyster. The bonus is that you&#8217;re using one of the lightest media players on the Internet! If you decide to get brave (or just <em>more awesome</em>), you can explore additional features. You&#8217;re using jQuery, so your possibilities are nearly limitless. If you want to implement a &#8220;Stop&#8221; feature (which returns the audio back to the beginning when &#8220;Stop&#8221; is pressed), you can get creative:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#stop-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">currentTime</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// return the audio file back to the beginning</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you want to include some volume controls, those can be added in a snap as well:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#volumeUp&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">volume</span> <span style="color: #339933;">+=</span><span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#volumeDown&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">volume</span> <span style="color: #339933;">-=</span><span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Try it out and let me know what you think. Your homework is to come up with some unique audio player functionality and share it here!</p>
<p>-Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/script-clip-html5-audio-player-with-jquery-controls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.ogg" length="4222877" type="audio/ogg" />
<enclosure url="http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.mp3" length="2680757" type="audio/mpeg" />
		</item>
		<item>
		<title>Web Development &#8211; JavaScript Packaging</title>
		<link>http://blog.softlayer.com/2013/web-development-javascript-packaging/</link>
		<comments>http://blog.softlayer.com/2013/web-development-javascript-packaging/#comments</comments>
		<pubDate>Thu, 10 Jan 2013 20:15:05 +0000</pubDate>
		<dc:creator>Philip Thompson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[dependencies]]></category>
		<category><![CDATA[efficient]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Mootools]]></category>
		<category><![CDATA[object oriented]]></category>
		<category><![CDATA[organization]]></category>
		<category><![CDATA[packaging]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[version control]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10401</guid>
		<description><![CDATA[If you think of JavaScript as the ugly duckling of programming languages, think again! It got a bad rap in the earlier days of the web because developers knew enough just to get by but didn&#8217;t really respect it like they did Java, PHP or .Net. Like other well-known and heavily used languages, JavaScript contains [...]]]></description>
			<content:encoded><![CDATA[<p>If you think of JavaScript as the ugly duckling of programming languages, think again! It got a bad rap in the earlier days of the web because developers knew enough just to get by but didn&#8217;t really respect it like they did Java, PHP or .Net. Like other well-known and heavily used languages, JavaScript contains various data types (String, Boolean, Number, etc.), objects and functions, and it is even capable of inheritance. Unfortunately, that functionality is often overlooked, and many developers seem to implement it as an afterthought: &#8220;Oh, we need to add some neat jQuery effects over there? I&#8217;ll just throw some inline JavaScript here.&#8221; That kind of implementation perpetuates a stereotype that JavaScript code is unorganized and difficult to maintain, but it doesn&#8217;t have to be! I&#8217;m going to show you how easy it is to maintain and organize your code base by packaging your JavaScript classes into a single file to be included with your website.</p>
<p>There are a few things to cover before we jump into code: </p>
<ol>
<li><strong>JavaScript Framework</strong> &#8211; <a href="http://mootools.net/">Mootools</a> is my framework of choice, but you can use whatever <a href="http://en.wikipedia.org/wiki/Comparison_of_JavaScript_frameworks">JavaScript framework</a> you&#8217;d like.</li>
<li><strong>Classes</strong> &#8211; Because I see JavaScript as another programming language that I respect (and is capable of object-oriented-like design), I write classes for EVERYTHING. Don&#8217;t think of your JavaScript code as something you use once and throw away. Write your code to be generic enough to be reused wherever it&#8217;s placed. Object-oriented design is great for this! Mootools makes object-oriented design easy to do, so this point reinforces the point above.</li>
<li><strong>Class Files</strong> &#8211; Just like you&#8217;d organize your PHP to contain one class per file, I do the exact same thing with JavaScript. <em>Note: Each of the class files in the example below uses the class name appended with <code>.js</code>.</em></li>
<li><strong>Namespacing</strong> &#8211; I will be organizing my classes in a way that will only add a single property &mdash; PT &mdash; to the global namespace. I won&#8217;t get into the details of namespacing in this blog because I&#8217;m sure you&#8217;re already thinking, &#8220;The code! The code! Get on with it!&#8221; You can namespace whatever is right for your situation.</li>
</ol>
<p>For this example, our classes will be food-themed because &#8230; well &#8230; I enjoy food. Let&#8217;s get started by creating our base object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
---
name: PT
description: The base class for all the custom classes
authors: [Philip Thompson]
provides: [PT]
...
*/</span>
<span style="color: #003366; font-weight: bold;">var</span> PT <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We now have an empty object from which we&#8217;ll build all of our classes. We&#8217;ll go I will go into more details later about the comment section, but let&#8217;s build our first class: <strong>PT.Ham</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
---
name: PT.Ham
description: The ham class
authors: [Philip Thompson]
requires: [/PT]
provides: [PT.Ham]
...
*/</span>
&nbsp;
<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>
    PT.<span style="color: #660066;">Ham</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// Custom code here...</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;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As I mentioned in point three (above), PT.Ham should be saved in the file named PT.Ham.js. When we create second class, <strong>PT.Pineapple</strong>, we&#8217;ll store it in PT.Pineapple.js:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
---
name: PT.Pineapple
description: The pineapple class
authors: [Philip Thompson]
requires: [/PT]
provides: [PT.Pineapple]
...
*/</span>
&nbsp;
<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>
    PT.<span style="color: #660066;">Pineapple</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// Custom code here...</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;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our final class for this example will be <strong>PT.Pizza</strong> (I&#8217;ll let you guess the name of the file where PT.Pizza lives). Our PT.Pizza class will require that PT, PT.Ham and PT.Pineapple be present.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
---
name: PT.Pizza
description: The pizza class
authors: [Philip Thompson]
requires: [/PT, /PT.Ham, /PT.Pineapple]
provides: [PT.Pizza]
...
*/</span>
&nbsp;
<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>
    PT.<span style="color: #660066;">Pizza</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// Custom code here that uses PT.Ham and PT.Pineapple...</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;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Before we go any further, let&#8217;s check out the comments we include above each of the classes. The comments are formatted for <a href="http://www.yaml.org/">YAML</a> &mdash; YAML Ain&#8217;t Markup Language (you gotta love recursive acronyms). These comments allow our parser to determine how our classes are related, and they help resolve dependencies. YAML&#8217;s pretty easy to learn and you only need to know a few basic features to use it. The YAML comments in this example are essential for our JavaScript package-manager &mdash; <a href="https://github.com/anutron/packager">Packager</a>. I won&#8217;t go into all the details about Packager, but simply mention a few commands that we&#8217;ll need to build our single JavaScript file.</p>
<p>In addition to the YAML comments in each of the class files, we also need to create a YAML file that will organize our code. This file &mdash; <strong>package.yml</strong> for this example &mdash; is used to load our separate JavaScript classes:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;PT&quot;</span>
description<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Provides our fancy PT classes&quot;</span>
authors<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;[Philip Thompson]&quot;</span>
version<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;1.0.0&quot;</span>
sources<span style="color: #339933;">:</span>
    <span style="color: #339933;">-</span> js<span style="color: #339933;">/</span>PT.<span style="color: #660066;">js</span>
    <span style="color: #339933;">-</span> js<span style="color: #339933;">/</span>PT.<span style="color: #660066;">Ham</span>.<span style="color: #660066;">js</span>
    <span style="color: #339933;">-</span> js<span style="color: #339933;">/</span>PT.<span style="color: #660066;">Pineapple</span>.<span style="color: #660066;">js</span>
    <span style="color: #339933;">-</span> js<span style="color: #339933;">/</span>PT.<span style="color: #660066;">Pizza</span>.<span style="color: #660066;">js</span></pre></div></div>

<p>package.yml shows that all of our PT* files are located in the js directory, one directory up from the package.yml file. Some of the properties in the YAML file are optional, and you can add much more detail if you&#8217;d like, but this will get the job done for our purposes. </p>
<p>Now we&#8217;re ready to turn back to Packager to build our packaged file. Packager includes an option to use PHP, but we&#8217;re just going to do it command-line. First, we need to register the new package (package.yml) we created for PT. If our JavaScript files are located in <code>/path/to/web/directory/js</code>, the package.yml file is in <code>/path/to/web/directory</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>packager register <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>web<span style="color: #000000; font-weight: bold;">/</span>directory</pre></div></div>

<p>This finds our package.yml file and registers our PT package. Now that we have our package registered, we can build it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>packager build <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>web<span style="color: #000000; font-weight: bold;">/</span>directory<span style="color: #000000; font-weight: bold;">/</span>js<span style="color: #000000; font-weight: bold;">/</span>PT.all.js</pre></div></div>

<p>The Packager sees that our PT package is registered, so it looks at each of the individual class files to build a single large file. In the comments of each of the class files, it determines if there are dependencies and warns you if any are not found. </p>
<p>It might seem like a lot of work when it&#8217;s written out like this, but I can assure you that when you go through the process, it takes no time at all. The huge benefit of packaging our JavaScript is evident as soon as you start incorporating those JavaScript classes into your website &#8230; Because we have built all of our class files into a single file, we don&#8217;t need to include each of the individual JavaScript files into our website (much less include the inline JavaScript declarations that make you cringe). To streamline your implementation even further if you&#8217;re using your JavaScript package in a production deployment, I recommend that you <a href="http://blog.softlayer.com/2011/html5-javascript-web-development-part-i/">&#8220;minify&#8221;</a> your code as well.</p>
<p>See &#8230; Organized code is no longer just for server-side only languages. Treat your JavaScript kindly, and it will be your friend!</p>
<p>Happy coding!</p>
<p>-Philip</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/web-development-javascript-packaging/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tips and Tricks &#8211; Pure CSS Sticky Footers</title>
		<link>http://blog.softlayer.com/2012/tips-and-tricks-pure-css-sticky-footers/</link>
		<comments>http://blog.softlayer.com/2012/tips-and-tricks-pure-css-sticky-footers/#comments</comments>
		<pubDate>Tue, 06 Nov 2012 20:00:17 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[compatibility]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[footer]]></category>
		<category><![CDATA[height]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[sticky footer]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=9491</guid>
		<description><![CDATA[By now, if you&#8217;ve seen my other blog posts, you know that I&#8217;m fascinated with how much JavaScript has evolved and how much you can do with jQuery these days. I&#8217;m an advocate of working smarter, not harder, and that maxim knows no coding language limits. In this post, I want to share a pure [...]]]></description>
			<content:encoded><![CDATA[<p>By now, if you&#8217;ve seen my other blog posts, you know that I&#8217;m fascinated with how much <a href="http://blog.softlayer.com/?s=javascript">JavaScript</a> has evolved and how much you can do with <a href="http://blog.softlayer.com/?s=jquery">jQuery</a> these days. I&#8217;m an advocate of working smarter, not harder, and that maxim knows no coding language limits. In this post, I want to share a pure CSS solution that allows for &#8220;sticky&#8221; footers on a web page. In comparing several different techniques to present this functionality, I found that all of the other routes were overkill when it came to processing time and resource usage.</p>
<p>Our objective is simple: Make the footer of our web page stay at the bottom even if the page&#8217;s content area is shorter than the user&#8217;s browser window.</p>
<p>This, by far, is one of my *favorite* things to do. It makes the web layout so much more appealing and creates a very professional feel. I ended up kicking myself the very first time I tried to add this functionality to a project early in my career (ten years ago &#8230; already!?) when I found out just how easy it was. I take solace in knowing that I&#8217;m not alone, though &#8230; A quick search for &#8220;footer stick bottom&#8221; still yields quite a few results from fellow developers who are wrestling with the same frustrating experience I did. If you&#8217;re in that boat, fear no more! We&#8217;re going to your footers in shape in a snap.</p>
<p>Here&#8217;s a diagram of the problem:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/headerfootercss.jpg" alt="CSS Footer"/></p>
<p>Unfortunately, a lot of people try to handle it with setting a fixed height to the content which would push the footer down. This may work when YOU view it, but there are several different browser window heights, resolutions and variables that make this an *extremely* unreliable solution (notice the emphasis on the word &#8220;extremely&#8221; &#8230; this basically means &#8220;don&#8217;t do it&#8221;).</p>
<p>We need a dynamic solution that is able to adapt on the fly to the height of a user&#8217;s browser window regardless if the resize it, have Firebug open, use a unique resolution or just have a really, really weird browser!</p>
<p>Let&#8217;s take a look at what the end results should look like:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/headerfootercss2.jpg" alt="CSS Footer"/></p>
<p>To make this happen, let&#8217;s get our HTML structure in place first:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;page&quot;</span>&gt;</span>
&nbsp;
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;header&quot;</span>&gt;</span> <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main&quot;</span>&gt;</span> <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;footer&quot;</span>&gt;</span> <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>It&#8217;s pretty simple so far &#8230; Just a skeleton of a web page. The <code>page</code> div contains ALL elements and is immediately below the <body> tags in the page code hierarchy. The <code>header</code> div is going to be our top content, the <code>main</code> div will include all of our content, and the <code>footer</code> div is all of our copyrights and footer links.</p>
<p>Let&#8217;s start by coding the CSS for the full page:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">Html<span style="color: #00AA00;">,</span> body <span style="color: #00AA00;">&#123;</span>
      Padding<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
      Margin<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
      Height<span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Adding a 100% height allows us to set the height of the <code>main</code> div later. The height of a div can only be as tall as the parent element encasing it. Now let&#8217;s see how the rest of our ids are styled:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#page</span> <span style="color: #00AA00;">&#123;</span>
      Min-<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
      <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#main</span> <span style="color: #00AA00;">&#123;</span>
      Padding-<span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span>   <span style="color: #808080; font-style: italic;">/* This value is the height of your footer */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #cc00cc;">#footer</span> <span style="color: #00AA00;">&#123;</span>
      Position<span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
      Width<span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>
      Bottom<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
      Height<span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span>  <span style="color: #808080; font-style: italic;">/* This value is the height of your footer */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>These rules position the footer &#8220;absolutely&#8221; at the bottom of the page, and because we set <code>#page</code> to <code>min-height: 100%</code>, it ensures that <code>#main</code> is exactly the height of the browser&#8217;s viewing space. One of the best things about this little trick is that it&#8217;s compliant with all major current browsers &mdash; including Firefox, Chrome, Safari *AND* Internet Explorer (after a little tweak). For Internet Explorer to not throw a fit, we need concede that IE doesn&#8217;t recognize <code>min-height</code> as a valid property, so we have to add <code>Height: 100%;</code> to <code>#page</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#page</span> <span style="color: #00AA00;">&#123;</span>
      Min-<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>  <span style="color: #808080; font-style: italic;">/* for all other browsers */</span>
      <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100%</span><span style="color: #00AA00;">;</span>  <span style="color: #808080; font-style: italic;">/* for IE */</span>
      <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>If the user does not have a modern, popular browser, it&#8217;s still okay! Though their old browser won&#8217;t detect the magic we&#8217;ve done here, it&#8217;ll fail gracefully, and the footer will be positioned directly under the content, as it would have been without our little CSS trick.</p>
<p>I can&#8217;t finish this blog without mentioning my FAVORITE perk of this trick: Should you not have a specially designed mobile version of your site, this trick even works on smart phones!</p>
<p>-Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/tips-and-tricks-pure-css-sticky-footers/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Tips and Tricks &#8211; jQuery Lazy Load Plugin</title>
		<link>http://blog.softlayer.com/2012/tips-and-tricks-jquery-lazy-load-plugin/</link>
		<comments>http://blog.softlayer.com/2012/tips-and-tricks-jquery-lazy-load-plugin/#comments</comments>
		<pubDate>Wed, 26 Sep 2012 14:00:25 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[HMTL]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[loading]]></category>
		<category><![CDATA[optimize]]></category>
		<category><![CDATA[page load]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=9346</guid>
		<description><![CDATA[In the late 90&#8242;s, web pages presented their information in a relatively structured fashion, with little concern on how &#8220;pretty&#8221; the content looked. To a certain extent, that was a result of available technology and resources being a little more limited, but much of the reason was probably because we had no idea what was [...]]]></description>
			<content:encoded><![CDATA[<p>In the late 90&#8242;s, web pages presented their information in a relatively structured fashion, with little concern on how &#8220;pretty&#8221; the content looked. To a certain extent, that was a result of available technology and resources being a little more limited, but much of the reason was probably because we had no idea what was possible. We&#8217;ve come a long way, my friend. These days, it&#8217;s tough to spend an hour online without coming across a gorgeous web site with huge animations, a pallet of every color possible, full-width backgrounds and high definition detail.</p>
<p>Those sites may be aesthetically pleasing, but they can be a big pain from a developer&#8217;s perspective. </p>
<p>How much load does all of that stuff put on the server every time that web page is visited? As developers, it&#8217;s our job to think about both what the visitor sees AND the visitor&#8217;s experience in seeing it. Even the most beautiful sites will be ignored if a page takes too long to load. We spend hours optimizing every detail so users can fluidly browse without having to wait. It was in one of these optimization sessions that I discovered &#8220;lazy load.&#8221; </p>
<p>To be honest, I wasn&#8217;t too fond of the word &#8220;lazy&#8221; in the name, and I especially wasn&#8217;t fond of having to explain to my boss that *I* wasn&#8217;t being lazy &#8230; The jQuery plugin  is *named* &#8220;<a href="http://www.appelsiini.net/projects/lazyload">Lazy Load</a>.&#8221; Lazy Load effectively allows large pieces of content to stay in the backlog until they&#8217;re needed. To give you an example of what that looks like, let&#8217;s say you have a website with three humungous images, but they&#8217;re all in different locations. Instead of pushing the entire load onto the user when they first land on your page, we can break them up and have them load only when the user goes to view them. We&#8217;re not reducing the size of the web page; we&#8217;re merely helping it work smarter.</p>
<p>Without Lazy Load, a normal web page loads each item when its page is visited. If a website has videos, music, images and some neat user interactivity applications, each of those items will load at the same time: </p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/lazyload.jpg" alt="Lazy Load Illustration"/></p>
<p>If you take into consideration how large each of those items are, you can sense the problem. The user only has so much bandwidth to load these items, and something&#8217;s gotta give. Usually, it means long loading times. We can&#8217;t control how fast each user&#8217;s ISP is, but we <em>can</em> reorder our items and let Lazy Load help us prioritize items and load the page more efficiently.</p>
<p>After we snag <a href="https://raw.github.com/tuupola/jquery_lazyload/master/jquery.lazyload.js">Lazy Load on Github</a> (jquery.lazyload.js), we put our jQuery scripts in the <code>&lt;head&gt;</code> of our page:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;jquery.js&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;jquery.lazyload.js&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Now that the plugin is available to us, we need to determine what we want to load lazily. Images are probably one of the most bothersome page elements, so let&#8217;s apply Lazy Load to the images we load in the <code>belazy</code> class. In the <code>&lt;head&gt;</code> of your page (or in the footer if you prefer your JavaScript entries there), you&#8217;ll add:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img.belazy&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">lazyload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>As a result of that function, all image tags with a class of <code>belazy</code> will have Lazy Load run on them. This helps us ensure that we&#8217;re not loading ALL of our images lazily. Now we need to choose which images we want to apply Lazy Load to.</p>
<p>Let&#8217;s say the image tag of the largest image on one of our page looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bighonkingimage.png&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>To have the lazyload function apply to it, we just have to make a couple tweaks:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;belazy&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bighonkingimage.png&quot;</span> data-original<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bighonkingimage.png&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>We added <code>class="belazy"</code> to trigger the lazyload function, and we added <code>data-original="bighonkingimage.png"</code> to line up with the formatting required by the newest version of Lazy Load (it&#8217;s simply a repeat of the source).</p>
<p>When a user visits our page, bighonkingimage.png will load only when it&#8217;s needed!</p>
<p>Pretty neat, eh?</p>
<p>-Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/tips-and-tricks-jquery-lazy-load-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Web Development &#8211; JavaScript &#8211; Creating a Sticky Menu</title>
		<link>http://blog.softlayer.com/2012/web-development-javascript-creating-a-sticky-menu/</link>
		<comments>http://blog.softlayer.com/2012/web-development-javascript-creating-a-sticky-menu/#comments</comments>
		<pubDate>Wed, 23 May 2012 15:50:10 +0000</pubDate>
		<dc:creator>Philip Thompson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[display]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[sticky menu]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=8193</guid>
		<description><![CDATA[When designing websites, I like to focus on ease of use and accessibility for the end user. While creating your site to be friendly to screen readers and text-based browsers is a must, the accessibility I&#8217;m referring to is making it easy for your audience to navigate your site and perform certain common actions. By [...]]]></description>
			<content:encoded><![CDATA[<p>When designing websites, I like to focus on ease of use and accessibility for the end user. While creating your site to be friendly to screen readers and text-based browsers is a must, the accessibility I&#8217;m referring to is making it easy for your audience to navigate your site and perform certain common actions. By providing an easy interface for your users, you are immediately increasing your chances that they&#8217;ll return for more of your site&#8217;s goodness.</p>
<p>Thus far in our &#8220;Web Development&#8221; blog series, we&#8217;ve looked at <a href="http://blog.softlayer.com/2011/html5-javascript-web-development-part-i/">JavaScript Optimization</a>, <a href="http://blog.softlayer.com/2012/web-development-html5-custom-data-attributes/">HTML5 Custom Data Attributes</a>, <a href="http://blog.softlayer.com/2012/web-development-html5-web-fonts/">HTML5 Web Fonts</a> and using CSS to style the <a href="http://blog.softlayer.com/2012/web-development-css-highlight-selection/">Highlight Selection</a>. In this post, we&#8217;re going to create a &#8220;sticky&#8221; menu at the top of a page. As a user scrolls down, the menu will &#8220;stick&#8221; to the top and always be visible (think of Facebook&#8217;s Timeline view), allowing the user quicker access to clicking common links. With some simple HTML, CSS and JavaScript, you can have a sticky menu in no time.</p>
<style type="text/css">
code{color:blue;}
</style>
<p>Let&#8217;s start with our HTML. We&#8217;re going to have a simple header, menu and content section that we&#8217;ll throw in our <code>&lt;body&gt;</code> tag.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>header<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>My Header<span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>header<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>nav id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;menu&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>ul id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;menu-list&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span>Items<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>nav<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;content&quot;</span><span style="color: #339933;">&gt;</span>
    Some content
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>For brevity, I&#8217;ve shortened the content I show here, but the working example will have all the information. Now we can throw in some CSS to style our elements. The important part here is how the <code>&lt;nav&gt;</code> is styled.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">nav<span style="color: #666666; font-style: italic;">#menu {
</span>    background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#FFF;
</span>    clear<span style="color: #339933;">:</span> both<span style="color: #339933;">;</span>
    margin<span style="color: #339933;">:</span> 40px <span style="color: #cc66cc;">0</span> 80px <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    width<span style="color: #339933;">:</span> <span style="color:#800080;">99.8</span><span style="color: #339933;">%;</span>
    z<span style="color: #339933;">-</span>index<span style="color: #339933;">:</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
ul<span style="color: #666666; font-style: italic;">#menu-list li {
</span>    border<span style="color: #339933;">:</span> solid 1px blue<span style="color: #339933;">;</span>
    list<span style="color: #339933;">-</span>style<span style="color: #339933;">-</span>type<span style="color: #339933;">:</span> none<span style="color: #339933;">;</span>
    display<span style="color: #339933;">:</span> inline<span style="color: #339933;">-</span>block<span style="color: #339933;">;</span>
    margin<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">-</span>3px<span style="color: #339933;">;</span>
    padding<span style="color: #339933;">:</span> 4px 10px<span style="color: #339933;">;</span>
    width<span style="color: #339933;">:</span> auto<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We have set the menu&#8217;s <code>background</code> to white (<code>#FFF</code>) and given it a <code>z-index</code> of <code>2</code> so that when the user scrolls, the menu will stay on top and not be see-through. We&#8217;ve also set the list items to be styled <code>inline-block</code>, but you can style your items however you desire.</p>
<p>Now we get to the fun part – the JavaScript. I&#8217;ve created a class using <a href="http://mootools.net/">Mootools</a>, but similar functionality could be achieved using your favorite JavaScript framework. Let&#8217;s examine our <code>initialize</code> method (our constructor) in our <code>Stickit</code> class.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Stickit <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Stickit</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// 'item' is our nav#menu in this case</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">id</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// The element we're scrolling will be the window</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">scrollTarget</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">id</span><span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">scrollTarget</span> <span style="color: #339933;">||</span> document.<span style="color: #660066;">window</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// The 'anchor' is an empty element that will always keep the same location</span>
        <span style="color: #006600; font-style: italic;">// when the user scrolls. This is needed because this.item will change and</span>
        <span style="color: #006600; font-style: italic;">// we cannot rely on it for accurate calculations.</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">anchor</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">inject</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'top'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// The 'filler' is an empty element that we'll use as a space filler for when</span>
        <span style="color: #006600; font-style: italic;">// the 'item' is being manipulated - this will prevent the content below from</span>
        <span style="color: #006600; font-style: italic;">// jumping around when we scroll.</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">filler</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">inject</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'after'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Set the styles of our 'filler' to match the styles of the 'item'</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setFillerStyles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Initialize our scroll events – see the next code section for details</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">initEvents</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;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What we&#8217;re doing here is grabbing our element to stick to the top – in this case, <code>nav#menu</code> – and initializing our other important elements. I&#8217;ll review these in the next code section.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Stickit <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">Stickit</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    ...
    <span style="color: #660066;">initEvents</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: #003366; font-weight: bold;">var</span> that <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span>
            <span style="color: #006600; font-style: italic;">// Grab the position of the anchor to be used for comparison during vertical scroll</span>
            anchorOffsetY <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">anchor</span>.<span style="color: #660066;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>
            <span style="color: #006600; font-style: italic;">// Grab our original styles of our 'item' so that we can reset them later</span>
            originalStyles <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">getStyles</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'margin-top'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'position'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'top'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// This is the function we'll provide as our scroll event handler</span>
        <span style="color: #003366; font-weight: bold;">var</span> stickit <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>
            <span style="color: #006600; font-style: italic;">// Determine if we have scrolled beyond our threshold - in this case, our</span>
            <span style="color: #006600; font-style: italic;">// anchor which is located as the first element of our 'item'</span>
            <span style="color: #003366; font-weight: bold;">var</span> targetScrollY <span style="color: #339933;">=</span> that.<span style="color: #660066;">scrollTarget</span>.<span style="color: #660066;">getScroll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>
                fixit <span style="color: #339933;">=</span> targetScrollY <span style="color: #339933;">&gt;</span> anchorOffsetY<span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>fixit <span style="color: #339933;">&amp;&amp;</span> that.<span style="color: #660066;">cache</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'fixed'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// If we have scrolled beyond the threshold, fix the 'item' to the top</span>
                <span style="color: #006600; font-style: italic;">// of the window with the following styles: margin-top, position and top</span>
                that.<span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">setStyles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #3366CC;">'margin-top'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
                    position<span style="color: #339933;">:</span> <span style="color: #3366CC;">'fixed'</span><span style="color: #339933;">,</span>
                    top<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">// Show our (empty) filler so that the content below the 'item' does not</span>
                <span style="color: #006600; font-style: italic;">// jump - this would otherwise be distracting to the user</span>
                that.<span style="color: #660066;">filler</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'block'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">// Cache our value so that we only set the styles when we need to</span>
                that.<span style="color: #660066;">cache</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'fixed'</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: #339933;">!</span>fixit <span style="color: #339933;">&amp;&amp;</span> that.<span style="color: #660066;">cache</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'default'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #006600; font-style: italic;">// We have not scrolled beyond the threshold.</span>
                <span style="color: #006600; font-style: italic;">// Hide our filler</span>
                that.<span style="color: #660066;">filler</span>.<span style="color: #660066;">setStyle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">// Reset the styles to our 'item'</span>
                that.<span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">setStyles</span><span style="color: #009900;">&#40;</span>originalStyles<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">// Cache our values so we don't keep resetting the styles</span>
                that.<span style="color: #660066;">cache</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'default'</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// Add our scroll event to the target - the 'window' in this case</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">scrollTarget</span>.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'scroll'</span><span style="color: #339933;">,</span> stickit<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// Fire our scroll event so that all the elements and styles are initialized</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">scrollTarget</span>.<span style="color: #660066;">fireEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'scroll'</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>This method contains the meat of our functionality. The logic includes that we test how far the user has scrolled down on the page. If s/he scrolls past the threshold – in this case, the anchor which is located at the very top of the &#8220;stuck&#8221; item – then we set the menu to be fixed to the top of the page by setting the CSS values for <code>margin-top</code>, <code>position</code> and <code>top</code>. We also display a filler so that the content below the menu doesn&#8217;t jump when we set the menu&#8217;s <code>position</code> to <code>fixed</code>. When the user scrolls back to the top, the styles are reset to their original values and the filler is hidden. </p>
<p>To see a full working example, check out this <a href="http://jsfiddle.net/MrA85/4/">fiddle</a>. The <code>Stickit</code> class I created is flexible enough so that you can &#8220;stick&#8221; any element to the top of the page, and you can specify a different <code>scrollTarget</code>, which will allow you to scroll another element (besides the <code>window</code>) and allow the item to stick to the top of that element instead of the <code>window</code>. If you want to give that a try, you can specify different options in <code>Stickit</code> and modify your CSS as needed to get it working as you&#8217;d like.</p>
<p>Happy coding,</p>
<p>-Philip</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/web-development-javascript-creating-a-sticky-menu/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Web Development &#8211; CSS &#8211; Highlight Selection</title>
		<link>http://blog.softlayer.com/2012/web-development-css-highlight-selection/</link>
		<comments>http://blog.softlayer.com/2012/web-development-css-highlight-selection/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 21:07:08 +0000</pubDate>
		<dc:creator>Philip Thompson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[display]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[highlight]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=7431</guid>
		<description><![CDATA[I immediately fell in love with CSS when we were introduced in late 2000. The ability to style a whole site outside the HTML was a fantastic concept and probably my first true introduction to separation of style and content. Put your words over here, and put how you display those words over there. So [...]]]></description>
			<content:encoded><![CDATA[<p>I immediately fell in love with CSS when we were introduced in late 2000. The ability to style a whole site outside the HTML was a fantastic concept and probably my first true introduction to separation of style and content. Put your words over here, and put how you display those words over there. So simple! Since then I have always been an advocate of cascading style sheets. Today&#8217;s tip will involve an effortless addition that will have your readers say, &#8220;Ooooh. That&#8217;s a clever little change.&#8221;</p>
<p>I find that when I read articles and blogs online, I not only read with my eyes, I scan the page with my mouse. Especially if it&#8217;s a wordy article or not styled in smaller columns, I highlight the text by clicking and dragging to help me maintain my focus. Up until recently, whenever you selected text that way in your browser, your operating system would choose the color of the background highlight. For Windows, this is generally blue. For OS X, this is whatever you&#8217;ve set your preferences to (which is light blue by default).</p>
<p>For those of you that use a newer version of Webkit (Chrome or Safari) or Gecko (Firefox), the site designer can determine what color to highlight your selection of text, and CSS has made it easy.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Webkit */</span>
<span style="color: #339933;">::</span><span style="color: #004000;">selection</span> <span style="color: #009900;">&#123;</span>
    background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#972F2C;
</span>    color<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#FFF;
</span><span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/* Gecko/Mozilla */</span>
<span style="color: #339933;">::-</span>moz<span style="color: #339933;">-</span>selection <span style="color: #009900;">&#123;</span>
    background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#972F2C;
</span>    color<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#FFF;
</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>As of today, Webkit browsers are the only ones that support <code>::selection</code> without browser prefixing. Firefox requires the <code>-moz-</code> prefix. Here we have set the highlight background color to &#8220;SoftLayer Red&#8221; (<code>#972F2C</code>) and made the text color white (<code>#FFF</code>). It should be noted that earlier versions of Webkit and Gecko did not support anything but the <code>background</code> property. There is still limited support for which CSS properties are allowed during selection. You are unable to change <code>font-style</code>, <code>font-size</code>, <code>text-decoration</code> and many other properties, but we can hope support for most of the properties will be available in the future.</p>
<p>This is pretty cool so far, but we can take it one small step further. Just like other selectors, we can apply the <code>::selection</code> selector to other elements and style each one differently.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">h2<span style="color: #339933;">::</span><span style="color: #004000;">selection</span> <span style="color: #009900;">&#123;</span>
    background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#B72E33;
</span>    color<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#FFF;
</span><span style="color: #009900;">&#125;</span>
p<span style="color: #339933;">::</span><span style="color: #004000;">selection</span> <span style="color: #009900;">&#123;</span>
    background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#ACEFB2;
</span><span style="color: #009900;">&#125;</span>
div<span style="color: #339933;">::</span><span style="color: #004000;">selection</span> <span style="color: #009900;">&#123;</span>
    background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#E4DB80;
</span><span style="color: #009900;">&#125;</span>
span<span style="color: #339933;">::</span><span style="color: #004000;">selection</span> <span style="color: #009900;">&#123;</span>
    background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#C780E4;
</span>    color<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#FFF;
</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>This produces the following:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/csshighlightingexample.png" alt="Highlighting Example"/></p>
<p>Surprise your readers and give them some highlight goodness.</p>
<p>Happy coding!</p>
<p>-Philip</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/web-development-css-highlight-selection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Web Development &#8211; HTML5 &#8211; Custom Data Attributes</title>
		<link>http://blog.softlayer.com/2012/web-development-html5-custom-data-attributes/</link>
		<comments>http://blog.softlayer.com/2012/web-development-html5-custom-data-attributes/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 17:30:29 +0000</pubDate>
		<dc:creator>Philip Thompson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[data attributes]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[form validation]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[update]]></category>
		<category><![CDATA[walkthrough]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=6843</guid>
		<description><![CDATA[I recently worked on a project that involved creating promotion codes for our clients. I wanted to make this tool as simple as possible to use and because this involved dealing with thousands of our products in dozens of categories with custom pricing for each of these products, I had to find a generic way [...]]]></description>
			<content:encoded><![CDATA[<p>I recently worked on a project that involved creating promotion codes for our clients. I wanted to make this tool as simple as possible to use and because this involved dealing with thousands of our products in dozens of categories with custom pricing for each of these products, I had to find a generic way to deal with client-side form validation. I didn&#8217;t want to write custom JavaScript functions for each of the required inputs, so I decided to use custom data attributes.</p>
<p>Last month, we started a series focusing on web development tips and tricks with a post about <a href="http://blog.softlayer.com/2011/html5-javascript-web-development-part-i/">JavaScript optimization</a>. In this installment, we&#8217;re cover how to use HTML5 custom data attributes to assist you in validating forms.</p>
<p><a href="http://www.w3.org/TR/html5/elements.html#custom-data-attribute">Custom data attributes</a> for elements are &#8220;[attributes] in no namespace whose name starts with the string &#8216;data-&#8217;, has at least one character after the hyphen, is XML-compatible, and contains no characters in the range U+0041 to U+005A (LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z).&#8221; Thanks W3C. That definition is bookish, so let&#8217;s break it down and look at some examples.</p>
<style type="text/css">
code{color:blue;}
</style>
<p style="margin-bottom:0; padding-bottom:0;"><strong>Valid:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div data<span style="color: #339933;">-</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Philip&quot;</span><span style="color: #339933;">&gt;</span>Mr<span style="color: #339933;">.</span> Thompson is an okay guy<span style="color: #339933;">.&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;softlayer.com&quot;</span> data<span style="color: #339933;">-</span>company<span style="color: #339933;">-</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SoftLayer&quot;</span> data<span style="color: #339933;">-</span>company<span style="color: #339933;">-</span>state<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;TX&quot;</span><span style="color: #339933;">&gt;</span>SoftLayer<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>li data<span style="color: #339933;">-</span>color<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;blue&quot;</span><span style="color: #339933;">&gt;</span>Smurfs<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span></pre></div></div>

<p style="margin-bottom:0; padding-bottom:0;"><strong>Invalid:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// This attribute is not prefixed with 'data-'</span>
    <span style="color: #339933;">&lt;</span>h2 database<span style="color: #339933;">-</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;244&quot;</span><span style="color: #339933;">&gt;</span>Food<span style="color: #339933;">&lt;/</span>h2<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// These 2 attributes contain capital letters in the attribute names</span>
    <span style="color: #339933;">&lt;</span>p data<span style="color: #339933;">-</span>firstName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Ashley&quot;</span> data<span style="color: #339933;">-</span>lastName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Thompson&quot;</span><span style="color: #339933;">&gt;...&lt;/</span>p<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// This attribute does not have any valid characters following 'data-'</span>
    <span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/images/pizza.png&quot;</span> data<span style="color: #339933;">-=</span><span style="color: #0000ff;">&quot;Sausage&quot;</span> <span style="color: #339933;">/&gt;</span></pre></div></div>

<p style="margin-bottom:0;padding-bottom:0;">Now that you know what custom data attributes are, why would we use them? Custom attributes allow us to relate specific information to particular elements. This information is hidden to the end user, so we don&#8217;t have to worry about the data cluttering screen space and we don&#8217;t have to create separate hidden elements whose purpose is to hold custom data (which is just bad practice). This data can be used by a JavaScript programmer to many different ends. Among the most common use cases are to manipulate elements, provide custom styles (using CSS) and perform form validation. In this post, we&#8217;ll focus on form validation. </p>
<p><span id="more-6843"></span></p>
<p>Let&#8217;s start out with a simple form with two radio inputs. Since I like food, our labels will be food items!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;radio&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;food&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;food&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>show<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;.pizza&quot;</span> <span style="color: #339933;">/&gt;</span> Pizza
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;radio&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;food&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sandwich&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;food&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>show<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#sandwich&quot;</span> <span style="color: #339933;">/&gt;</span> Sandwich
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden required&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>error<span style="color: #339933;">-</span>food<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>A food item must be selected<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Here we have two standard radio inputs with some custom data attributes and a hidden div (using CSS) that contains our error message. The first input has a name of <code>food</code> and a value of <code>pizza</code> &ndash; these will be used on the server side once our form is submitted. There are two custom data attributes for this input: <code>data-sl-required</code> and <code>data-sl-show</code>. I am defining the data attribute <code>data-sl-required</code> to indicate that this radio button is required in the form and one of them must be selected to pass validation. Note that I have prefixed <code>required</code> with <code>sl-</code> to <a href="http://en.wikipedia.org/wiki/Namespace">namespace</a> my data attribute. <code>required</code> is generic and I don&#8217;t want to have any conflicts with any other attributes – especially ones written for other projects! The value of <code>data-sl-required</code> is <code>food</code>, which I have tied to the div with the attribute <code>data-sl-error-food</code> (more on this later). </p>
<p>The second custom attribute is <code>data-sl-show</code> with a selector of <code>.pizza</code>. (To review selectors, jump back to the <a href="http://blog.softlayer.com/2011/html5-javascript-web-development-part-i/">JavaScript optimization</a> post.) This data attribute will be used to show a hidden div that contains the class <code>pizza</code> when the radio button is clicked. The <code>sandwich</code> radio input has the same data attributes but with slightly different values.</p>
<p style="margin-bottom:0;padding-bottom:0;">Now we can move on to our HTML with the hidden text inputs:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden pizza&quot;</span><span style="color: #339933;">&gt;</span>
    Pizza type<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pizza&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>depends<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{&amp;quot;type&amp;quot;:&amp;quot;radio&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;food&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;sandwich&amp;quot;}&quot;</span> <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden required&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>error<span style="color: #339933;">-</span>pizza<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>The pizza type must not be empty<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sandwich&quot;</span><span style="color: #339933;">&gt;</span>
    Sandwich<span style="color: #339933;">:</span> <span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sandwich&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>required<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sandwich&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>depends<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{&amp;quot;type&amp;quot;:&amp;quot;radio&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;food&amp;quot;,&amp;quot;value&amp;quot;:&amp;quot;sandwich&amp;quot;}&quot;</span> <span style="color: #339933;">/&gt;</span>
    <span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hidden required&quot;</span> data<span style="color: #339933;">-</span>sl<span style="color: #339933;">-</span>error<span style="color: #339933;">-</span>sandwich<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>The sandwich must not be empty<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p style="margin-bottom:0;padding-bottom:0;">These are hidden divs that contain text inputs that will be used to input more data depending on the radio input selected. Notice that the first outer div has a class of <code>pizza</code> and the second outer div has an id of <code>sandwich</code>. These values tie back to the <code>data-sl-show</code> selectors for the radio inputs. These text inputs also contain the <code>data-sl-required</code> attribute like the previous radio inputs. The <code>data-sl-depends</code> data attribute is a fun attribute that contains a JSON-encoded object that&#8217;s been run through PHP&#8217;s <em>htmlentities()</em> function. For readability, the <code>data-sl-depends</code> values contain:</p>
<div style="margin-left:20px;margin-top:0;"><code>{"type":"radio","name":"food","value":"pizza"}</code><br />
<code>{"type":"radio","name":"food","value":"sandwich"}</code></div>
<p>This first attribute says that our text input “depends” on the <code>radio</code> input with the name <code>food</code> and value <code>pizza</code> to be visible and selected in order to be processed as required. You can just imagine the possibilities of combinations you can create to make very custom functionality with very generic JavaScript.</p>
<p>Now we can examine our JavaScript to make sense of all these custom data attributes. Note that I&#8217;ll be using Mootools&#8217; syntax, but the same can fairly easily be accomplished using jQuery or your favorite JavaScript framework. I&#8217;m going to start by creating a <em>DataForm</em> class. This will be generic enough so that you can use it in multiple forms and it&#8217;s not tied to this specific instance. Reuse is good! To have it fit our needs, we&#8217;re going to pass some options when instantiating it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">new</span> DataForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    ...<span style="color: #339933;">,</span>
    dataAttributes<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        required<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data-sl-required'</span><span style="color: #339933;">,</span>
        errorPrefix<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data-sl-error'</span><span style="color: #339933;">,</span>
        depends<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data-sl-depends'</span><span style="color: #339933;">,</span>
        show<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data-sl-show'</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>As you can see, I&#8217;m using the data attribute names from our form as the options – this will allow you to create your own data attribute names depending on your uses. We first need to make our hidden divs visible whenever our radio buttons are clicked – we&#8217;ll use our <em>initData()</em> method for that.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">initData<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: #003366; font-weight: bold;">var</span> attrs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">options</span>.<span style="color: #660066;">dataAttributes</span><span style="color: #339933;">,</span>
        divs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=radio]['</span><span style="color: #339933;">+</span>attrs.<span style="color: #660066;">show</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']'</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>input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span>input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>attrs.<span style="color: #660066;">show</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        divs.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>div<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        input.<span style="color: #660066;">addEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'change'</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>
            divs.<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>div<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> div.<span style="color: #660066;">hide</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>
            div.<span style="color: #660066;">show</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></pre></div></div>

<p>This method grabs all the radio inputs with the <em>show</em> attribute (<code>data-sl-show</code>) and adds an <em>onchange</em> event to each of them. When a radio input is checked, it first hides all the divs, and then shows the div that&#8217;s associated with that radio input.</p>
<p>Great! Now we have our text inputs showing and hiding depending on which radio button is selected. Now onto the actual validation. First, we&#8217;ll make sure our required radio inputs are checked:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=radio]['</span><span style="color: #339933;">+</span>attrs.<span style="color: #660066;">required</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']:not(:checked)'</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>input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    checkError<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> isRadioWithNameSelected<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This grabs all the unchecked radio inputs with the <em>required</em> attribute (<code>data-sl-required</code>) and checks to see if any radio with that same name is selected using the function <em>isRadioWithNameSelected()</em>. The <em>checkError()</em> function will show or hide the error div (<code>data-sl-error-*</code>) depending if the radio is checked or not. Don&#8217;t worry &#8211; you&#8217;ll see how these functions are implemented in the JSFiddle below.</p>
<p>Now we can check our text inputs:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=text]['</span><span style="color: #339933;">+</span>attrs.<span style="color: #660066;">required</span><span style="color: #339933;">+</span><span style="color: #3366CC;">']:visible'</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>input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">name</span> <span style="color: #339933;">=</span> input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        depends <span style="color: #339933;">=</span> input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>attrs.<span style="color: #660066;">depends</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        value <span style="color: #339933;">=</span> input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</span><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>depends<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        depends <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">encode</span><span style="color: #009900;">&#40;</span>depends<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>depends.<span style="color: #660066;">type</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'radio'</span><span style="color: #339933;">:</span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>depends.<span style="color: #000066;">name</span> <span style="color: #339933;">&amp;&amp;</span> depends.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> radio <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=radio][name=&quot;'</span><span style="color: #339933;">+</span>depends.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;][value=&quot;'</span><span style="color: #339933;">+</span>depends.<span style="color: #660066;">value</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;]:checked'</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: #339933;">!</span>radio<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        checkError<span style="color: #009900;">&#40;</span>input.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span>attrs.<span style="color: #660066;">required</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: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    checkError<span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> value<span style="color: #339933;">!=</span><span style="color: #3366CC;">''</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></div></div>

<p>This obtains the required and visible text inputs and determines if they are empty or not. Here we look at our <code>depends</code> object to grab the associated radio inputs. If that radio is checked and the text input is empty, the error message appears. If that radio is not checked, it doesn&#8217;t even evaluate that text input. The <code>depends.type</code> is in a <em>switch</em> statement to allow for easy expansion of types. There are other cases for evaluating relationships &#8230; I&#8217;ll let you come up with more for yourself.</p>
<p>That concludes our usage of custom data attributes in form validation. This really only touched upon the very tip of the iceberg. Data attributes allow you &ndash; the creative developer &ndash; to interact in a new, HTML-valid way with your web pages.</p>
<p>Check out the working example of the above code at <a href="http://jsfiddle.net/philthathril/YyKbt/1/">jsfiddle.net</a>. To read more on custom data attributes, see what Google has to say. If you want to see really cool functionality that uses data attributes plus so much more, check out Aaron Newton&#8217;s <a href="http://www.clientcide.com/code-releases/clientcide-3-0-behavior-1-0-mootools-development-and-so-much-more/">Behavior module</a> over at <a href="http://www.clientcide.com/">clientcide.com</a>.</p>
<p>Happy coding!</p>
<p>-Philip</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/web-development-html5-custom-data-attributes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
