<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.2" -->
<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/"
	>

<channel>
	<title>ShopDev Website Design Blog</title>
	<link>http://www.shopdev.co.uk/blog</link>
	<description>Website Design, XHTML, CSS, jQuery and CubeCart</description>
	<pubDate>Sat, 28 Feb 2009 20:19:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.2</generator>
	<language>en</language>
			<item>
		<title>Password Strength Meter</title>
		<link>http://www.shopdev.co.uk/blog/password-strength-meter/</link>
		<comments>http://www.shopdev.co.uk/blog/password-strength-meter/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 20:16:56 +0000</pubDate>
		<dc:creator>Homar</dc:creator>
		
		<category><![CDATA[General Resources]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.shopdev.co.uk/blog/password-strength-meter/</guid>
		<description><![CDATA[I was scouring the internet looking for a freely available "Password Strength Meter" script.  You've probably all come across them.  The vast majority are useless.  They're useless because they use mainly empirical methods to determine the strength of a password.  Whilst empirical methods may provide a good estimate for the strength of a password subject to a brute force attack.  Most don't even consider dictionary or hybrid-dictionary attack mechanisms.  So... I decided to make my own.]]></description>
			<content:encoded><![CDATA[<p>I was scouring the internet looking for a freely available &quot;Password Strength Meter&quot; script.&nbsp; You&#8217;ve probably all come across them.&nbsp; The vast majority are useless.&nbsp; They&#8217;re useless because they use mainly empirical methods to determine the strength of a password.&nbsp; Whilst empirical methods may provide a good estimate for the strength of a password subject to a brute force attack&#8230;&nbsp; Most don&#8217;t consider dictionary or hybrid-dictionary attack mechanisms.</p>
<p>If you&#8217;ve signed up for a Google account, you will have seen Google&#8217;s own password strength meter.&nbsp; Google&#8217;s password strength meter is unique in that it identifies whether the password would be easily cracked under a dictionary attack.&nbsp; However, Google hasn&#8217;t made the source code available to the public.&nbsp; So&#8230; I thought I&#8217;d make my own.<a class="demo" title="Password Strength Meter Demo" target="_blank" href="http://www.shopdev.co.uk/blog/demos/password-strength/index.html" rel="nofollow"><span class="demo website" style="float:none;"></span></a></p>
<p>&nbsp;</p>
<p>Firstly, I wanted my algorithm to calculate the strength of a password using statistical analysis.&nbsp; Essentially, the algorithm should approximate the ammount of time it would take to crack a password subject to the following attack mechanisms:</p>
<ul>
<li>Dictionary Attack</li>
<li>Hybrid-Dictionary</li>
<li>Brute Force</li>
</ul>
<p>Before I run over the math, I should say that I am not a statictician, although I am pretty good with applied mathematics.</p>
<h3>Dictionary Attack</h3>
<p>In order to calculate the strength of a password subject to a dictionary attack, we must start by determining how many &quot;real&quot; words exist containing 1 <em>to</em> n letters; where &quot;n&quot; is the length of the password.&nbsp; We should also consider that the password will, on average, find the password after searching through half of the n-letter words.&nbsp; We can express this mathematically as follows:</p>
<p>&nbsp;<img height="109" width="510" alt="" src="http://www.shopdev.co.uk/blog/wp-content/uploads/image/Untitled-1_03.gif" /></p>
<h3>Brute Force Attack</h3>
<p>In order to calculate the strength of a password subject to a brute force attack, we simply consider the number of character combinations possible for a password of length 1 <em>to</em> n letters.&nbsp; The number of possibilities exitsing is a function of both the password length and character diversity.&nbsp; In this sense, the diversity of a password depends on whether upper case letters, lower case letters, numbers and symbols are used.&nbsp; The diversity of a password containing only lower case letters is 26.&nbsp; This is because there are 26 letters in the english alphabet.</p>
<p><img height="110" width="510" alt="" src="http://www.shopdev.co.uk/blog/wp-content/uploads/image/Untitled-1_06.gif" /></p>
<p>Note that we again take the coefficient of a half for the final term.&nbsp; I explained why this is done in the dictionary attack section above.&nbsp; What I found very interesting is how the strength of a password subject to a brute force attack varies as we change the diversity and length.&nbsp; Many of the algorithms I have come across give a greater weight to the diversity.&nbsp; I.e. they assume that the diversity of a password has a greater impact on the strength.&nbsp; This is often not the case.&nbsp; My only guess as to why they do this is that they have no way of identifying whether a password is vulnerable to a dictionary attack.&nbsp; Hence, they make the assumption that passwords containing numbers and/or symbols will not be vulnerable to a dictionary attack.</p>
<h3>Hybrid-Dictionary Attack</h3>
<p>This type of attack is particularly difficult to model accurately.&nbsp; Hence, I am going to estimate that the strength of a password undergoing a hybrid dictionary attack is simply the &quot;dictionary strength&quot; multiplied by the &quot;brute force strength&quot; of the suffix/prefix:</p>
<p><img height="95" width="510" src="http://www.shopdev.co.uk/blog/wp-content/uploads/image/Untitled-1_08.gif" alt="" /></p>
<h3>Calculating The &quot;Real&quot; Strength</h3>
<p>The equations above allow us to calculate the strength of a password.&nbsp; However, we now need to make sense of the result.&nbsp; To do this, we can create a scale by estimating the speed at which a computer is capable of attempting to crack the password.&nbsp; Obviously, this value will change significantly as time passes as it is determined by the processing power of the cracker&#8217;s computer.&nbsp; Hence, I recommend that you change/tweak the setting in the PHP script.&nbsp; By default, this is set to 2,000,000 passwords per second.</p>
<p>The PHP script then uses this speed to calculate the time it would take to crack the password.&nbsp; I have defined a strength rating to a range in time (e.g. 0 &lt; time &lt; 1 day).&nbsp; You can change or add more ratings as you please.</p>
<h3>Where Can I Download This?</h3>
<p>Before I make the script public, I&#8217;d appreciate some feedback.&nbsp; You can request additional features by posting a comment below.&nbsp; Please also post any passwords that you feel the script does a poor job of estimating the strength for.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shopdev.co.uk/blog/password-strength-meter/feed/</wfw:commentRss>
		</item>
		<item>
		<title>jQuery From Scratch: jQuery Rollovers Using &#8220;this&#8221;</title>
		<link>http://www.shopdev.co.uk/blog/jquery-rollovers-using-this/</link>
		<comments>http://www.shopdev.co.uk/blog/jquery-rollovers-using-this/#comments</comments>
		<pubDate>Sat, 09 Aug 2008 23:57:44 +0000</pubDate>
		<dc:creator>Homar</dc:creator>
		
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.shopdev.co.uk/blog/jquery-rollovers-using-this/</guid>
		<description><![CDATA[If you’ve been programming in PHP, or other object oriented languages, you may have come across “this” or “self”.  With jQuery, we can write $(this) to select an element in the DOM (Document Object Model).  In this tutorial, I'll demonstrate how and why $(this) is useful.  To do this, we'll be writing a simple image rollover script.]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been programming in PHP, or other object oriented languages, you may have come across &#8220;this&#8221; or &#8220;self&#8221;.  With jQuery, we can write $(this) to select an element in the DOM (Document Object Model).  The best way to demonstrate this is with a simple example.</p>
<p>Let&#8217;s write a short script to do the classic image rollover:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<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;#menu img&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">hover</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: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-hover.&quot;</span><span style="color: #009900;">&#41;</span><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: #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: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-hover.&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You might have your markup something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;menu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item1.gif&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item2.gif&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item3.gif&quot;</span> <span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span></pre></div></div>

<p>So what does the code do?  Firstly, we initialize the document.ready() function - using the shorthand notation.  You can read more about this in the 1st <a rel="nofollow" href="http://www.shopdev.co.uk/blog/jquery-from-scratch-a-beginners-guide/">jQuery tutorial</a>.  Within this function, we attach the &#8220;hover&#8221; event to the images which together create the menu.  You can see in the sample HTML markup that there are 3 images.  These images are contained within unordered list elements.</p>
<p>Look one line down from where we attach the hover event and you&#8217;ll see:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;-hover.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is where things start to get interesting.  $(this) is referencing the image that is hovered over.  So couldn&#8217;t we have just written $(&#8221;#menu img&#8221;) instead of $(this)?  Well&#8230; no.  This is because $(&#8221;#menu img&#8221;) will select all the images instead of just the image that is being hovered over.  We could have added a unique class (or ID) to each image and then have selected that for each image.  The problem with this is that it involves a whole lot more of unnecessary code.  We&#8217;d need to attach the hover event to each image.</p>
<p>The next part of that line is: &#8220;.attr(&#8230;)&#8221;.  We can use this to change a property to a defined value.  We can also use this to get the value of a defined property.  For example, the following code changes the SRC property to &#8220;foo.gif&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img#example&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;foo.gif&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can also assign the current value of the SRC property to a variable by writing:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> srcValue <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img#example&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the rollover code, we use both.  We assign the SRC property a new value by taking the current value, splitting it at the &#8220;.&#8221; (period) and joining &#8220;-hover.&#8221; onto the end.  You can read more about the <a rel="nofollow" href="http://www.tizag.com/javascriptT/javascript-string-split.php">&#8220;split&#8221; function</a>.  Essentially, this function splits a string into an array at the delimiter.  In this case, we define the &#8220;.&#8221; (period) as the delimiter.</p>
<p>We pretty much do the reverse for when the cursor moves off the image.  We define the delimiter as &#8220;-hover.&#8221;, thus removing these characters from the SRC property.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shopdev.co.uk/blog/jquery-rollovers-using-this/feed/</wfw:commentRss>
		</item>
		<item>
		<title>jQuery From Scratch: Functions</title>
		<link>http://www.shopdev.co.uk/blog/jquery-functions-tutorial/</link>
		<comments>http://www.shopdev.co.uk/blog/jquery-functions-tutorial/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 01:40:52 +0000</pubDate>
		<dc:creator>Homar</dc:creator>
		
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.shopdev.co.uk/blog/jquery-from-scratch-functions/</guid>
		<description><![CDATA[This is part 2 of the "jQuery From Scratch" series of tutorials. If you have not read part 1, you can have a quick read. This tutorial will deal with writing and executing functions in JavaScript.  jQuery does not provide a quick method for creating functions. That's because it doesn't need to. So what are functions? Instead of giving you the standard definition, I'll explain why/when you might want to create and use functions in your code.  Of course, I'll also explain how to write and execute functions.]]></description>
			<content:encoded><![CDATA[<p>This is part 2 of the &#8220;jQuery From Scratch&#8221; series of tutorials.  If you have not read part 1, you can have a <a rel="nofollow" href="http://www.shopdev.co.uk/blog/jquery-from-scratch-a-beginners-guide/" title="jQuery Beginners Guide" target="_blank">quick read</a>.  This tutorial will deal with writing and executing functions in JavaScript.</p>
<p>jQuery does not provide a quick method for creating functions.  That&#8217;s because it doesn&#8217;t need to.  So what are functions?  Instead of giving you the standard definition, I&#8217;ll explain why/when you might want to create and use functions in your code.</p>
<p>Say that you have a block of code that you would like executed on multiple events.  This code could do anything.  The principal to consider is that you will need this code to be executed as multiple events are triggered.  Instead of writing the code over and over again for each event, you could add this code to a function and then execute this function whenever you want the code to be executed.</p>
<h3>Creating Functions</h3>
<p>To create a function called &#8220;aFunction&#8221;, we could write:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> aFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Say that we wanted to alert the user each time this function is executed.  We could write:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> aFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this example, the user is alerted each time the function is executed.</p>
<h3>Executing Functions</h3>
<p>We can execute the function when any event is triggered.  If we wanted the function to be executed when the page has loaded, we could write:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">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>
  aFunction<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></pre></div></div>

<p>This is pretty straightforwards&#8230; but there&#8217;s more to functions.</p>
<h3>Passing Arguments</h3>
<p>We can pass values into the function that are used when the code is executed.  To demonstrate this, I&#8217;ll continue with the example that I&#8217;ve been using.  Instead of displaying &#8220;Hello World!&#8221; whenever the function is executed, we could pass the message into the function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> aFunction<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We could now execute the function as follows (to display any old message to the visitor) when the page has loaded:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">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>
  aFunction<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Just a short message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We could pass multiple arguments by separating them with a comma:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> addNumbers<span style="color: #009900;">&#40;</span>num1<span style="color: #339933;">,</span>num2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> total <span style="color: #339933;">=</span> num1 <span style="color: #339933;">+</span> num2<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> total<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The function above will simply return the total value of the two numbers passed as arguments.  It is important to note that the value returned will not by output to the page.  You could print the returned value to the page by using document.write:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">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>
  document.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>addNumbers<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.shopdev.co.uk/blog/jquery-functions-tutorial/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Sortable Lists Using jQuery UI</title>
		<link>http://www.shopdev.co.uk/blog/sortable-lists-using-jquery-ui/</link>
		<comments>http://www.shopdev.co.uk/blog/sortable-lists-using-jquery-ui/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 23:49:09 +0000</pubDate>
		<dc:creator>Homar</dc:creator>
		
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.shopdev.co.uk/blog/sortable-lists-using-jquery-ui/</guid>
		<description><![CDATA[Unordered lists have a number of useful applications.  They are commonly used to structure a site's navigation.  I have found, in a number of web applications, that the ability to reorder (sort) such lists would be extremely useful.  So, how can this be achieved?]]></description>
			<content:encoded><![CDATA[<p>Unordered lists have a number of useful applications.  They are commonly used to structure a site&#8217;s navigation.  I have found, in a number of web applications, that the ability to reorder (sort) such lists would be extremely useful.  So, how can this be achieved?</p>
<p>I&#8217;m a big fan of the jQuery library.  Since most of the applications I build use jQuery, developing with this library would be the most logical approach.  But wait&#8230;  It looks like there are 2 jQuery plugins that allow elements to be sorted.  These are the <a rel="nofollow" href="http://interface.eyecon.ro/" title="jQuery Interface" target="_blank">jQuery Interface</a> plugin and <a rel="nofollow" href="http://ui.jquery.com/home" title="jQuery UI" target="_blank">jQuery UI</a>.</p>
<p>At first, I started working with &#8220;Interface&#8221;.  I soon ran into problems as I started working with styled lists.  For some reason, the list elements lost their styling as they were being dragged.  I just couldn&#8217;t quite put my finger on the problem - perhaps I missed something.  I then started working with jQuery UI.  This plugin retained the styling - perfect!</p>
<p>However, neither of these plugins provide a method to easily save and reload the list element order.  Hence, should a user change the order of the list, these changes would be lost as the page is refreshed.<br />
<a rel="nofollow" href="http://www.shopdev.co.uk/blog/sortables.html" class="demo" target="_blank" title="Sortable Lists"><span class="demo"></span></a><br style="line-height: 0px" clear="all" /><br style="line-height: 0px" clear="all" /></p>
<h3>Saving the Reordered List</h3>
<p>I determined that the easiest way to accomplish this is through the use of a cookie.  The jQuery UI plugin already allows you to send the ID&#8217;s of the list elements (in order) to an array.  Using Klaus Hart&#8217;s Cookie plugin, we can easily save the list element order to a cookie.  But how do we retain/reload the element order saved to the cookie?</p>
<p>To restore the element order, I decided to remove all the sortable list elements from the DOM and then immediately append them to their parent element (in the order sepcified by the cookie).  This is all done after the document has loaded.</p>
<h3>The HTML Markup</h3>

<div class="wp_syntax"><div class="code"><pre class="html4strict"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;list1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item-1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>List Item 1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item-2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>List Item 2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item-3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>List Item 3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item-4&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>List Item 4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item-5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>List Item 5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;item-6&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>List Item 6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span></pre></div></div>

<h3>The Javascript</h3>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #006600; font-style: italic;color: #666666;">/////////////////////////////////////////////////////////////////</span>
<span style="color: #006600; font-style: italic;color: #666666;">/////  EDIT THE FOLLOWING VARIABLE VALUES  //////////////////////</span>
<span style="color: #006600; font-style: italic;color: #666666;">/////////////////////////////////////////////////////////////////</span>
&nbsp;
<span style="color: #006600; font-style: italic;color: #666666;">// set the list selector</span>
<span style="color: #003366; font-weight: bold;">var</span> setSelector <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#list1&quot;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;color: #666666;">// set the cookie name</span>
<span style="color: #003366; font-weight: bold;">var</span> setCookieName <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;listOrder&quot;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;color: #666666;">// set the cookie expiry time (days):</span>
<span style="color: #003366; font-weight: bold;">var</span> setCookieExpiry <span style="color: #339933;">=</span> <span style="color: #CC0000;">7</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;color: #666666;">/////////////////////////////////////////////////////////////////</span>
<span style="color: #006600; font-style: italic;color: #666666;">/////  YOU PROBABLY WON'T NEED TO EDIT BELOW  ///////////////////</span>
<span style="color: #006600; font-style: italic;color: #666666;">/////////////////////////////////////////////////////////////////</span>
&nbsp;
<span style="color: #006600; font-style: italic;color: #666666;">// function that writes the list order to a cookie</span>
<span style="color: #003366; font-weight: bold;">function</span> getOrder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;color: #666666;">// save custom order to cookie</span>
	$.<span style="color: #006600;">cookie</span><span style="color: #009900;">&#40;</span>setCookieName<span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span>setSelector<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">sortable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;toArray&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> expires<span style="color: #339933;">:</span> setCookieExpiry<span style="color: #339933;">,</span> path<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;color: #666666;">// function that restores the list order from a cookie</span>
<span style="color: #003366; font-weight: bold;">function</span> restoreOrder<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> list <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>setSelector<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>list <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>
&nbsp;
	<span style="color: #006600; font-style: italic;color: #666666;">// fetch the cookie value (saved order)</span>
	<span style="color: #003366; font-weight: bold;">var</span> cookie <span style="color: #339933;">=</span> $.<span style="color: #006600;">cookie</span><span style="color: #009900;">&#40;</span>setCookieName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>cookie<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;color: #666666;">// make array from saved order</span>
	<span style="color: #003366; font-weight: bold;">var</span> IDs <span style="color: #339933;">=</span> cookie.<span style="color: #006600;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;,&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;color: #666666;">// fetch current order</span>
	<span style="color: #003366; font-weight: bold;">var</span> items <span style="color: #339933;">=</span> list.<span style="color: #006600;">sortable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;toArray&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;color: #666666;">// make array from current order</span>
	<span style="color: #003366; font-weight: bold;">var</span> rebuild <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> v<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> len<span style="color: #339933;">=</span>items.<span style="color: #006600;">length</span><span style="color: #339933;">;</span> v<span style="color: #339933;">&lt;</span>len<span style="color: #339933;">;&gt;</span>
		rebuild<span style="color: #009900;">&#91;</span>items<span style="color: #009900;">&#91;</span>v<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> items<span style="color: #009900;">&#91;</span>v<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> IDs.<span style="color: #006600;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;color: #666666;">// item id from saved order</span>
		<span style="color: #003366; font-weight: bold;">var</span> itemID <span style="color: #339933;">=</span> IDs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>itemID <span style="color: #000066; font-weight: bold;">in</span> rebuild<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;color: #666666;">// select item id from current order</span>
			<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">=</span> rebuild<span style="color: #009900;">&#91;</span>itemID<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;color: #666666;">// select the item according to current order</span>
			<span style="color: #003366; font-weight: bold;">var</span> child <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul.ui-sortable&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;color: #666666;">// select the item according to the saved order</span>
			<span style="color: #003366; font-weight: bold;">var</span> savedOrd <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul.ui-sortable&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">children</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span> <span style="color: #339933;">+</span> itemID<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;color: #666666;">// remove all the items</span>
			child.<span style="color: #006600;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;color: #666666;">// add the items in turn according to saved order</span>
			<span style="color: #006600; font-style: italic;color: #666666;">// we need to filter here since the &quot;ui-sortable&quot;</span>
			<span style="color: #006600; font-style: italic;color: #666666;">// class is applied to all ul elements and we</span>
			<span style="color: #006600; font-style: italic;color: #666666;">// only want the very first!  You can modify this</span>
			<span style="color: #006600; font-style: italic;color: #666666;">// to support multiple lists - not tested!</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ul.ui-sortable&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:first&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006600;">append</span><span style="color: #009900;">&#40;</span>savedOrd<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>
&nbsp;
<span style="color: #006600; font-style: italic;color: #666666;">// code executed when the document loads</span>
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;color: #666666;">// here, we allow the user to sort the items</span>
	$<span style="color: #009900;">&#40;</span>setSelector<span style="color: #009900;">&#41;</span>.<span style="color: #006600;">sortable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		axis<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;y&quot;</span><span style="color: #339933;">,</span>
		cursor<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;move&quot;</span><span style="color: #339933;">,</span>
		update<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> getOrder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;color: #666666;">// here, we reload the saved order</span>
	restoreOrder<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></pre></div></div>

<p>There are 2 functions to be concerned with in the above code.  The first, &#8220;getOrder()&#8221;, writes the order of the list elements to a cookie.  The second, &#8220;restoreOrder()&#8221;, fetches the order saved to the cookie and restores that order.  We execute &#8220;getOrder()&#8221; each time the list order is changed.  We execute &#8220;restoreOrder()&#8221; after the document has loaded.</p>
<p>Using the code provided above, you should only need to change the variables (at the very top) to get this working with your page.  Just remember to include the jQuery library and jQuery UI plugin!  I hope this is helpful!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shopdev.co.uk/blog/sortable-lists-using-jquery-ui/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
