<?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; jQuery</title>
	<atom:link href="http://blog.softlayer.com/tag/jquery/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>Wed, 15 May 2013 15:33:34 +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>Tips and Tricks – Building a jQuery Plugin (Part 2)</title>
		<link>http://blog.softlayer.com/2013/tips-and-tricks-building-a-jquery-plugin-part-2/</link>
		<comments>http://blog.softlayer.com/2013/tips-and-tricks-building-a-jquery-plugin-part-2/#comments</comments>
		<pubDate>Thu, 14 Feb 2013 15:25:43 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[exercise]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10619</guid>
		<description><![CDATA[jQuery plugins don&#8217;t have to be complicated to create. If you&#8217;ve stumbled upon this blog in pursuit of a guide to show you how to make a jQuery plugin, you might not believe me &#8230; It seems like there&#8217;s a chasm between the &#8220;haves&#8221; of jQuery plugin developers and the &#8220;have nots&#8221; of future jQuery [...]]]></description>
			<content:encoded><![CDATA[<p>jQuery plugins don&#8217;t have to be complicated to create. If you&#8217;ve stumbled upon this blog in pursuit of a guide to show you how to make a jQuery plugin, you might not believe me &#8230; It seems like there&#8217;s a chasm between the &#8220;haves&#8221; of jQuery plugin developers and the &#8220;have nots&#8221; of future jQuery developers, and there aren&#8217;t very many bridges to get from one side to the other. In <a href="http://blog.softlayer.com/2012/tips-and-tricks-building-a-jquery-plugin-part-1/">Part 1</a> of our &#8220;Building a jQuery Plugin&#8221; series, we broke down how to build the basic structure of a plugin, and in this installment, we&#8217;ll be adding some usable functionality to our plugin. </p>
<p>Let&#8217;s start with the jQuery code block we created in Part 1:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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: #660066;">fn</span>.<span style="color: #660066;">slPlugin</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                myVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;This is&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// this will be the default value of this var</span>
                anotherVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;our awesome&quot;</span><span style="color: #339933;">,</span>
                coolVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;plugin!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                ourString <span style="color: #339933;">=</span> myVar <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> anotherVar <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> coolVar<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: #000066; font-weight: bold;">return</span> ourString<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We want our plugin to do a little more than return, &#8220;This is our awesome plugin!&#8221; so let&#8217;s come up with some functionality to build. For this exercise, let&#8217;s create a simple plugin that allows truncates a blob of text to a specified length while providing the user an option show/hide the rest of the text. Since the most common character length limitation on the Internet these days is Twitter&#8217;s 140 characters, we&#8217;ll use that mark in our example.</p>
<p>Taking what we know about the basic jQuery plugin structure, let&#8217;s create the foundation for our new plugin &mdash; <code>slPlugin2</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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: #660066;">fn</span>.<span style="color: #660066;">slPlugin2</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            length<span style="color: #339933;">:</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span>
            moreLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;read more&quot;</span><span style="color: #339933;">,</span>
            lessLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;collapse&quot;</span><span style="color: #339933;">,</span>
            trailingText<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, we&#8217;ve established four default variables:</p>
<ul>
<li><code>length</code>: The length of the paragraph we want before we truncate the rest.</li>
<li><code>moreLength</code>: What we append to the paragraph when it is truncated. This will be the link the user clicks to expand the rest of the text.</li>
<li><code>lessLink</code>: What we append to the paragraph when it is expanded. This will be the link the user clicks to collapse the rest of the text.</li>
<li><code>trailingText</code>: The typical ellipses to append to the truncation.</li>
</ul>
<p>In our jQuery plugin example from Part 1, we started our function with <code>this.each(function() {</code>, and for this example, we&#8217;re going to add a return for <code>this</code> to maintain chainability. By doing so, we&#8217;re able to manipulate the segment with methods. For example, if we started our function with <code>this.each(function() {</code>, we&#8217;d call it with this line:</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;">'#ourParagraph'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slPlugin2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If we start the function with <code>return this.each(function() {</code>, we have the freedom to add further manipulation:</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;">'#ourParagraph'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slPlugin2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>With such a simple change, we&#8217;re able to add method calls to make one massive dynamic function. </p>
<p>Let&#8217;s flesh out the actual function a little more. We&#8217;ll add a substantial bit of code in this step, but you should be able to follow along with the changes via the comments:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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: #660066;">fn</span>.<span style="color: #660066;">slPlugin2</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            length<span style="color: #339933;">:</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span> 
            moreLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;read more&quot;</span><span style="color: #339933;">,</span>
            lessLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;collapse&quot;</span><span style="color: #339933;">,</span>
            trailingText<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// return this keyword for chainability</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> ourText <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// the element we want to manipulate</span>
            <span style="color: #003366; font-weight: bold;">var</span> ourHtml <span style="color: #339933;">=</span> ourText.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//get the contents of ourText!</span>
            <span style="color: #006600; font-style: italic;">// let's check if the contents are longer than we want</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ourHtml.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> options.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> truncSpot <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #339933;">,</span> options.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// the location of the first space (so we don't truncate mid-word) where we will end our truncation.</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">// make sure to ignore the first space IF the text starts with a space</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>truncSpot <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #006600; font-style: italic;">// the part of the text that will not be truncated, starting from the beginning</span>
       <span style="color: #003366; font-weight: bold;">var</span> firstText <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> truncSpot<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #006600; font-style: italic;">// the part of the text that will be truncated, minus the trailing space</span>
       <span style="color: #003366; font-weight: bold;">var</span> secondText <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>truncSpot<span style="color: #339933;">,</span> ourHtml.<span style="color: #660066;">legnth</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Are you still with us? I know it seems like a lot to take in, but each piece is very straightforward. The <code>firstText</code> is the chunk of text that will be shown: The first 140 characters (or whatever <code>length</code> you define). The <code>secondText</code> is what <em>will</em> be truncated. We have two blobs of text, and now we need to make them work together:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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: #660066;">fn</span>.<span style="color: #660066;">slPlugin2</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            length<span style="color: #339933;">:</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span> 
            moreLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;read more&quot;</span><span style="color: #339933;">,</span>
            lessLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;read less&quot;</span><span style="color: #339933;">,</span>
            trailingText<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// return this keyword for chainability</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> ourText <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// the element we want to manipulate</span>
            <span style="color: #003366; font-weight: bold;">var</span> ourHtml <span style="color: #339933;">=</span> ourText.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//get the contents of ourText!</span>
            <span style="color: #006600; font-style: italic;">// let's check if the contents are longer than we want</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ourHtml.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> options.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> truncSpot <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #339933;">,</span> options.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// the location of the first space (so we don't truncate mid-word) where we will end our truncation.</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">// make sure to ignore the first space IF the text starts with a space</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>truncSpot <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #006600; font-style: italic;">// the part of the text that will not be truncated, starting from the beginning</span>
       <span style="color: #003366; font-weight: bold;">var</span> firstText <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> truncSpot<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #006600; font-style: italic;">// the part of the text that will be truncated, minus the trailing space</span>
       <span style="color: #003366; font-weight: bold;">var</span> secondText <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>truncSpot<span style="color: #339933;">,</span> ourHtml.<span style="color: #660066;">legnth</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #006600; font-style: italic;">// perform our truncation on our container ourText, which is technically more of a &quot;rewrite&quot; of our paragraph, to our liking so we can modify how we please. It's basically saying: display the first blob then add our trailing text, then add our truncated part wrapped in span tags (to further modify)</span>
       ourText.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>firstText <span style="color: #339933;">+</span> options.<span style="color: #660066;">trailingText</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;span class=&quot;slPlugin2&quot;&gt;'</span> <span style="color: #339933;">+</span> secondText <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #006600; font-style: italic;">// but wait! The secondText isn't supposed to show until the user clicks &quot;read more&quot;, right? Right! Hide it using the span tags we wrapped it in above.</span>
       ourText.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.slPlugin2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;none&quot;</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;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our function now truncates text to the specified length, and we can call it from our page simply:</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;jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<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;jquery.slPlugin2.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
$(document).ready(function() {  
    $('#slText').slPlugin2();  
});
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>Out of all the ways to truncate text via jQuery, this has to be my favorite. It&#8217;s feature-rich while still being fairly easy to understand. As you might have noticed, we haven&#8217;t touched on the &#8220;read more&#8221; and &#8220;read less&#8221; links or the expanding/collapsing animations yet, but we&#8217;ll be covering those in Part 3 of this series. Between now and when Part 3 is published, I challenge you to think up how you&#8217;d add those features to this plugin as homework. </p>
<p>-Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/tips-and-tricks-building-a-jquery-plugin-part-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</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; Building a jQuery Plugin (Part 1)</title>
		<link>http://blog.softlayer.com/2012/tips-and-tricks-building-a-jquery-plugin-part-1/</link>
		<comments>http://blog.softlayer.com/2012/tips-and-tricks-building-a-jquery-plugin-part-1/#comments</comments>
		<pubDate>Tue, 27 Nov 2012 20:40:09 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[creating]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[lazyload]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Select2]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=9780</guid>
		<description><![CDATA[I&#8217;ve written several blogs detailing the use of different jQuery plugins (like Select2, LazyLoad and equalHeights), and in the process, I&#8217;ve noticed an increasing frustration among the development community when it comes to building jQuery plugins. The resources and documentation I&#8217;ve found online have not as clear and easy as they could be, so in [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written several blogs detailing the use of different jQuery plugins (like <a href="http://blog.softlayer.com/2012/tips-and-tricks-jquery-select2-plugin/">Select2</a>, <a href="http://blog.softlayer.com/2012/tips-and-tricks-jquery-lazy-load-plugin/">LazyLoad</a> and <a href="http://blog.softlayer.com/2012/tips-and-tricks-jquery-equalheights-plugin/">equalHeights</a>), and in the process, I&#8217;ve noticed an increasing frustration among the development community when it comes to building jQuery plugins. The resources and documentation I&#8217;ve found online have not as clear and easy as they could be, so in my next few posts, I&#8217;ll break down the process to make jQuery plugin creation simple and straightforward. In this post, we&#8217;ll cover the basic structure of a plugin and where to insert your own functionality, and in Part 2, we&#8217;ll pick a simple task and add on to our already-made structure.</p>
<p>Before I go any further, it&#8217;s probably important to address a question you might be asking yourself: &#8220;Why would I want to make my own plugin?&#8221; The best reason that comes to my mind is portability. If you&#8217;ve ever created a large-scale project, take a look back into your source code and note how many of the hundreds of lines of jQuery code you could put into a plugin to reuse on a different project. You probably invested a lot of time and energy into that code, so it doesn&#8217;t make sense to reinvent the wheel if you ever need that functionality again. If that&#8217;s not enough of a reason for you, I can also tell you that if you develop your own jQuery plugin, you&#8217;ll level-up in cool points, and the jQuery community will love you.</p>
<p>For this post, let&#8217;s create a jQuery plugin that simply returns, &#8220;This is our awesome plugin!&#8221; Our first step involves putting together the basic skeleton used by every plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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: #660066;">fn</span>.<span style="color: #660066;">slPlugin</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>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Awesome plugin stuff goes here</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is your template &mdash; your starting point. Practice it. Remember it. Love it. The &#8220;<code>slPlugin</code>&#8221; piece is what I chose to name this plugin. It&#8217;s best to name your plugin something unique &#8230; I always run a quick Google search to ensure I don&#8217;t duplicate the name of a plugin I (or someone else) might need to use in a project alongside my plugin. In this case, we&#8217;re calling the example plugin slPlugin because SoftLayer is awesome, and I like naming my plugins after awesome things. I&#8217;ll save this code in a file called jquery.slPlugin.js.</p>
<p>Now that we have our plugin&#8217;s skeleton, let&#8217;s add some default values for variables:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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: #660066;">fn</span>.<span style="color: #660066;">slPlugin</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                myVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;default&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// this will be the default value of this var</span>
                anotherVar<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
                coolVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;this is cool&quot;</span><span style="color: #339933;">,</span>                
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s look at the changes we made between the first example and this one. You&#8217;ll notice that in our second line we added &#8220;options&#8221; to become  <code>$.fn.slPlugin = function(options) {</code>. We do this because our function is now accepting arguments, and we need to let the function know that. The next difference you come across is the <code>var defaults</code> blurb. In this section, we&#8217;re providing default values for our variables. If you don&#8217;t define values for a given variable when you call the plugin, these default values will be used.</p>
<p>Now let&#8217;s have our plugin return the message we want to send:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><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: #660066;">fn</span>.<span style="color: #660066;">slPlugin</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                myVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;This is&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// this will be the default value of this var</span>
                anotherVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;our awesome&quot;</span><span style="color: #339933;">,</span>
                coolVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;plugin!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                ourString <span style="color: #339933;">=</span> myVar <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> anotherVar <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> coolVar<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: #000066; font-weight: bold;">return</span> ourString<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We&#8217;ve defined our default values for our variables, concatenated our variables and we&#8217;ve added a return under our variable declaration. If our jQuery plugin is included in a project and no values are provided for our variables, slPlugin will return, &#8220;This is our awesome plugin!&#8221; </p>
<p>It seems rather rudimentary at this point, but we have to crawl before we walk. This introductory post is laying the groundwork of coding a jQuery plugin, and we&#8217;ll continue building on this example in the next installment of this series. As you&#8217;ve seen with the LazyLoad, equalHeights and Select2, there are much more complicated things we can do with our plugin, and we&#8217;ll get there. Sneak Preview: In the next installment, we&#8217;ll be creating and implementing a truncation function for our plugin &#8230; Get excited!</p>
<p>-Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/tips-and-tricks-building-a-jquery-plugin-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tips and Tricks &#8211; jQuery Select2 Plugin</title>
		<link>http://blog.softlayer.com/2012/tips-and-tricks-jquery-select2-plugin/</link>
		<comments>http://blog.softlayer.com/2012/tips-and-tricks-jquery-select2-plugin/#comments</comments>
		<pubDate>Wed, 17 Oct 2012 19:50:27 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[developers]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[Select2]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=9467</guid>
		<description><![CDATA[Web developers have the unique challenge of marrying coding logic and visual presentation to create an amazing user experience. Trying to find a balance between those two is pretty difficult, and it&#8217;s easy to follow one or the other down the rabbit hole. What&#8217;s a web developer to do? I&#8217;ve always tried to go the [...]]]></description>
			<content:encoded><![CDATA[<p>Web developers have the unique challenge of marrying coding logic and visual presentation to create an amazing user experience. Trying to find a balance between those two is pretty difficult, and it&#8217;s easy to follow one or the other down the rabbit hole. What&#8217;s a web developer to do?</p>
<p>I&#8217;ve always tried to go the &#8220;work smarter, not harder&#8221; route, and when it comes to balancing functionality and aesthetics, that usually means that I look around for plugins and open source projects that meet my needs. In the process of sprucing up an form, I came across <a href="http://ivaynberg.github.com/select2/">jQuery Select2</a>, and it quickly became one of my favorite plugins for form formatting. With minimal scripting and little modification, you get some pretty phenomenal results. </p>
<p>We&#8217;ve all encountered drop-down selection menus on web forms, and they usually look like this:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/select.jpg" alt="Option Select"/></p>
<p>Those basic drop-downs meet a developer&#8217;s need for functionality, but they aren&#8217;t winning any beauty pageants. Beyond the pure aesthetic concerns, when a menu contains dozens (or hundreds) of selectable options, it becomes a little unwieldy. That&#8217;s why I was so excited to find Select2.</p>
<p>With Select2, you can turn the old, plain, boring-looking select boxes into beautiful, graceful and more-than-functional select widgets:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/selectpretty.jpg" alt="Pretty Option Select"/></p>
<p>Not only is the overall presentation of the data improved, Select2 also includes an auto-complete box. A user can narrow down the results quickly ad easily, and if you&#8217;ve got some of those endlessly scrolling select boxes of country names or currencies, your users will absolutely notice the change (and love you for it).</p>
<p>What&#8217;s even sexier than the form facelift is that you can add the plugin to your form in a matter of minutes.</p>
<p>After we <a href="https://github.com/ivaynberg/select2/tags">download Select2</a> and upload it to our box, we add our the jQuery library and scripts to the <code>&lt;head&gt;</code> of our document:</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;select2.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>For the gorgeous styling, we&#8217;ll also add Select2&#8242;s included style sheet:</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;">link</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;select2.css&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span><span style="color: #66cc66;">/</span>&gt;</span></pre></div></div>

<p>Before we close our <code>&lt;head&gt;</code> tag, we invoke the Select2 function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
	$<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;#selectPretty&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">select2</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: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>At this point, Select2 is locked and load, and we just have to add the <code>#selectPretty</code> ID to the <code>select</code> element we want to improve:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>select id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;selectPretty&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Option1&quot;</span><span style="color: #339933;">&gt;</span>Option <span style="color: #CC0000;">1</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Option2&quot;</span><span style="color: #339933;">&gt;</span>Option <span style="color: #CC0000;">2</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Option3&quot;</span><span style="color: #339933;">&gt;</span>Option <span style="color: #CC0000;">3</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;Option4&quot;</span><span style="color: #339933;">&gt;</span>Option <span style="color: #CC0000;">4</span><span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Notice: the <code>selectPretty</code> ID is what we defined when we invoked the Select2 function in our <code>&lt;head&gt;</code> tag.</p>
<p>With miniscule coding effort, we&#8217;ve made huge improvements to the presentation of our usually-boring select menu. It&#8217;s so easy to implement that even the most black-and-white coding-minded web developers can add some pizzazz to their next form without having to get wrapped up in styling!</p>
<p>-Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/tips-and-tricks-jquery-select2-plugin/feed/</wfw:commentRss>
		<slash:comments>7</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>Tips and Tricks &#8211; jQuery equalHeights Plugin</title>
		<link>http://blog.softlayer.com/2012/tips-and-tricks-jquery-equalheights-plugin/</link>
		<comments>http://blog.softlayer.com/2012/tips-and-tricks-jquery-equalheights-plugin/#comments</comments>
		<pubDate>Thu, 06 Sep 2012 20:05:31 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[developers]]></category>
		<category><![CDATA[divs]]></category>
		<category><![CDATA[equalHeights]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[height]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=9190</guid>
		<description><![CDATA[Last month, I posted a blog about dynamically resizing divs with jQuery, and we received a lot of positive feedback about it. My quest to avoid iframes proved to be helpful, so I thought I&#8217;d share a few more esoteric jQuery tips and tricks that may be of use to the developers and designers in [...]]]></description>
			<content:encoded><![CDATA[<p>Last month, I posted a blog about <a href="http://blog.softlayer.com/2012/no-iframes-dynamically-resize-divs-with-jquery/">dynamically resizing divs with jQuery</a>, and we received a lot of positive feedback about it. My quest to avoid iframes proved to be helpful, so I thought I&#8217;d share a few more esoteric jQuery tips and tricks that may be of use to the developers and designers in the audience. As I thought back about other challenges I&#8217;ve faced as a coder, a great example came to mind: Making divs equal height, regardless of the amount of content inside.</p>
<p>I haven&#8217;t seen many elegant div-based solutions for that relatively simple (and common) task, so I&#8217;ve noticed that many people struggle with it. Often, developers will turn back to the &#8220;Dark Side&#8221; of using tables to format the content since all columns would have the same height as the tallest column by default:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/jquery1.png" alt="JQuery Tutorial"/></p>
<p>It was easy theme table columns and to achieve the coveted 100% height that many designers seek, but emulating that functionality with divs proves to be much more difficult. A div is like the Superman of HTML elements (faster-loading, more flexible, more dynamic, etc.), and while it has super powers, it also has its own Kryptonite-like weaknesses &#8230; The one relevant to this blog post being that floating three div elements next to each other isn&#8217;t going to give you the look of a table:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/jquery2.png" alt="JQuery Tutorial"/></p>
<p>Each of the three divs has its own height, so if you&#8217;re doing something as simple as applying background colors, you&#8217;re going to wind up with an aesthetically unpleasing result: It&#8217;s going to look funky.</p>
<p>You could get into some nifty HTML/CSS workarounds, but many frustrated theme creators and designers will tell you that if your parent elements don&#8217;t have a height of a 100%, you&#8217;re just wasting coding lines. Some complex solutions create the illusion of all three divs being the same height (which is arguably better than setting fixed heights), but that complexity can be difficult to scale and repeat if you need to perform similar tasks throughout your site or your application. The easiest way to get the functionality you want and the simplicity you need: The <a href="http://www.cssnewbie.com/download/jquery.equalheights.js">jQuery equalHeights</a> plugin!</p>
<p>With a few class declarations in your existing HTML, you get the results you want, and with equalHeights, you can also specify the minimum and maximum parameters so it will create scrollable divs if the tallest element happens to be higher than your specified maximum.</p>
<h3>How to Use jQuery equalHeights</h3>
<p>First and foremost, include your JQuery lirbraries in the <code>&lt;HEAD&gt;</code> of your document:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;script language</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;jquery.equalheights.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The equalHeights plugin is not a hosted library, so you have to host the file on your server (<a href="http://www.cssnewbie.com/download/jquery.equalheights.js">here&#8217;s the link again</a>).</p>
<p>With the required libraries called in our document, it&#8217;s time to make the magic happen in your HTML.</p>
<p><strong>Create Your Divs</strong></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;divHeight&quot;</span>&gt;</span>This DIV is medium sized, not too big and not too small, but just right.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<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;divHeight&quot;</span>&gt;</span>This DIV has a lot of useful content and media that the user can interact with, thus it's very tall.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<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;divHeight&quot;</span>&gt;</span>This DIV is tiny. Period.<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>To have them line up next to each other, you&#8217;d have them <code>float:left;</code> in your CSS, and now you need to apply the equalHeights function.</p>
<p><strong>Call the equalHeights Plugin</strong><br />
In order for the script to recognize the height of the tallest element, you&#8217;d need to call <code>$(document).ready</code> just before the <code>&lt;/body&gt;</code> tag on your page. This will ensure that the page loads before the function runs. </p>
<p>The call looks like this:</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>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;.divHeight&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">equalHeights</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;">;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>If you want to specify a minimum and maximum (i.e. The div should be at least this tall and should be no taller than [adds scrollbar if the div size exceeds] the maximum), just add the parameters:</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>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;.divHeight&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">equalHeights</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">600</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;">;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>The initial call does not change the appearance of the divs, but the time it takes to do the resizing is so miniscule that users will never notice. After that call is made and the height is returned, each div with the class of <code>divHeight</code> will inherit the the same height, and your divs will be nice and pretty:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/jquery3.png" alt="JQuery Tutorial"/></p>
<p>This trick saved me a lot of headache and frustration, so hopefully it will do the same for you too!</p>
<p>-Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/tips-and-tricks-jquery-equalheights-plugin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>No iFrames! Dynamically Resize Divs with jQuery.</title>
		<link>http://blog.softlayer.com/2012/no-iframes-dynamically-resize-divs-with-jquery/</link>
		<comments>http://blog.softlayer.com/2012/no-iframes-dynamically-resize-divs-with-jquery/#comments</comments>
		<pubDate>Wed, 08 Aug 2012 20:30:20 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coder]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[developers]]></category>
		<category><![CDATA[divs]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[resize]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[update]]></category>
		<category><![CDATA[walkthrough]]></category>
		<category><![CDATA[width]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=8988</guid>
		<description><![CDATA[It&#8217;s no secret that iframes are one of the most hated methods of web page layouts in the web development world &#8212; they are horrible for SEO, user experience and (usually) design. I was recently charged with creating a page that needed functionality similar to what iframes would normally provide, and I thought I&#8217;d share [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s no secret that iframes are one of the most hated methods of web page layouts in the web development world &mdash; they are horrible for SEO, user experience and (usually) design. I was recently charged with creating a page that needed functionality similar to what iframes would normally provide, and I thought I&#8217;d share the non-iframe way I went about completing that project. </p>
<p>Before I get into the nitty-gritty of the project, I should probably unpack a few of the reasons <em>why</em> iframes are shunned. When a search engine indexes a page with iframes, each iframe is accurately recorded as a separate page &mdash; iframes embed the content of one we page inside of another, so it makes sense. Because each of those &#8220;pages&#8221; is represented in a single layout, if a user wanted to bookmark your site, they&#8217;d probably have a frustrating experience when they try to return to your site, only to find that they are sent directly to the content in one of the frames instead of seeing the entire layout. Most often, I see when when someone has a navigation bar in one frame and the main content in the other &#8230; The user will bookmark the content frame, and when they return to the site, they have no way to navigate the pages. So what&#8217;s a developer to do?</p>
<p>The project I was tasked with required the ability to resize only certain sections of a page, while asynchronously shrinking another section so that the entire page would always stay the same size, with only the two sections inside changing size. </p>
<p>Let&#8217;s look at an example with two divs, side by side on a web page:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/iframetutorial.png" alt="iFrame Tutorial"/></p>
<p>One div will contain a navigation menu to jump to different pages of the website (<code>#sidebar</code>), and the second div will contain all the content for that page (<code>#content</code>). If some of the elements in #sidebar are too long to read with the default width of the div, we want to let the user freely resize the two divs without changing the width of the page.</p>
<p>Our task is straightforward: When <code>#sidebar</code> expands in width, also expand the navigation and shrink <code>#content</code> along with the main content inside <code>#content</code>. If <code>#sidebar</code> shrinks, the navigation, <code>#content</code> and main content would respond accordingly as well:</p>
<p><img class="centered" src="http://cdn.softlayer.com/innerlayer/iframetutorial2.png" alt="iFrame Tutorial"/></p>
<p>It&#8217;s a relatively easy concept to do with iFrames &#8230; But then you remember that iframes are no longer cool (yes, there was a time long ago when iframes were cool). I decided to turn to my favorite alternative &mdash; jQuery &mdash; and the fix was actually a lot easier than I expected, and it worked beautifully. Let&#8217;s run through a step-by-step tutorial.</p>
<h3>1. HTML</h3>
<p>Lay out your two divs:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sidebar&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;sidebar-menu&quot;</span><span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;!--</span> all your sidebar<span style="color: #339933;">/</span>navigational items go here <span style="color: #339933;">--&gt;</span>
	<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>
<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>
	<span style="color: #339933;">&lt;!--</span> all your main content goes here <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<h3>2. CSS</h3>
<p>Style your divs:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#sidebar {
</span>       width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">49</span><span style="color: #339933;">%;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">#content {
</span>	width<span style="color: #339933;">:</span> <span style="color: #cc66cc;">49</span><span style="color: #339933;">%;</span>
        float<span style="color: #339933;">:</span> left<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>3. jQuery</h3>
<p>Now that we have our two divs side by side, let&#8217;s apply some jQuery magic. To do that, Let&#8217;s include our jQuery files in the <code>&lt;HEAD&gt;</code> of our document:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>link href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css&quot;</span> rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js&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: #0000ff;">&quot;http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Now that we have the necessary scripts, we can write our function:</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>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;#sidebar&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">resizable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>      
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sidebar &quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;resize&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> setWidth <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sidebar&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content).width(1224-setWidth);
            $('</span>.<span style="color: #660066;">menu</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span>setWidth<span style="color: #339933;">-</span><span style="color: #CC0000;">6</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: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>I know that might seem like an intimidating amount of information, so let&#8217;s break it down:</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;#sidebar&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">resizable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>      
   <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This portion simply makes the div with the ID of &#8220;sidebar&#8221; resizable (which accomplishes 33% of what we want it to do).</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;#sidebar &quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;resize&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> ui<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></pre></div></div>

<p>By using the <code>.bind</code>, we are able to trigger other events when <code>#sidebar</code> is called.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">            <span style="color: #003366; font-weight: bold;">var</span> setWidth <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sidebar&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content).width(1224-setWidth);</span></pre></div></div>

<p>This is where the magic happens. We&#8217;re grabbing the current width of <code>#sidebar</code> and subtracting it from the width you want your site to be. This code is what keeps your page stays the same width with only the divs changing sizes.</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;">'.menu).width(setWidth-6);</span></pre></div></div>

<p>This part of the code that expands the contents in the navigation along with <code>#sidebar</code>.</p>
<p>You can see a working example of iframe-like functionality with jQuery here: <a href="http://jqueryui.com/demos/resizable/">http://jqueryui.com/demos/resizable/</a></p>
<p>The only part you <em>won&#8217;t</em> find there is the trick to adjust a corresponding div&#8217;s size to make it grow/shrink with the first &#8230; I had a heck of a time searching that on the web, so hopefully this quick tutorial will help other developers who might be searching for this kind of functionality!</p>
<p>- Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/no-iframes-dynamically-resize-divs-with-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
