<?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; lazyload</title>
	<atom:link href="http://blog.softlayer.com/tag/lazyload/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>Tue, 04 Jun 2013 20:27:40 +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>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>
	</channel>
</rss>
