<?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>All Geekness Great and Small &#187; web</title>
	<atom:link href="http://www.danrumney.co.uk/tag/web/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.danrumney.co.uk</link>
	<description>Technology from work and home</description>
	<lastBuildDate>Mon, 26 Jul 2010 03:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Rotating Banners in random order</title>
		<link>http://www.danrumney.co.uk/2009/11/09/rotating-banners-in-random-order/</link>
		<comments>http://www.danrumney.co.uk/2009/11/09/rotating-banners-in-random-order/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 00:38:59 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=273</guid>
		<description><![CDATA[I was recently sent a script designed to take a series of advertising banners and rotate them on a page. By &#8216;rotate&#8217;, I mean display on banner in a designated position and then, after a certain period of time, replace it with another, and then another, and so on. To be fair to those who [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently sent a script designed to take a series of advertising banners and rotate them on a page. By &#8216;rotate&#8217;, I mean display on banner in a designated position and then, after a certain period of time, replace it with another, and then another, and so on. To be fair to those who paid for the banners, each banner was chosen at random so that each new visitor to the site would see a different banner first, second, third, etc. After taking a look at it, I spotted some problems and decided to fix them.<br />
<span id="more-273"></span></p>
<p>The main problem with the script is that it was <strong>too</strong> random. It took an array, or list, of banners and, on each rotation, it would select a random position in this list. The problem with this becomes obvious when you look at some examples of random number selections. The output from the following (Perl) script:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$idx</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$idx</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000066;">print</span> <span style="color: #000066;">int</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$idx</span><span style="color: #339933;">--;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;,&quot;</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$idx</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>is</p>
<p>0,1,2,0,3,0,3,3,3,3,0,3,0,1,1,1,0,4,4,2,4</p>
<p>If each number represents a specific banner, you can see that there is a significant bias toward 3 in this set of 20 rotations. This is not indicating an inherent bias towards 3. Quite the opposite; this is truly random, but human perception detects a bias because our sample is relatively low.</p>
<p>Leaving the statistics behind, it is quite easy to ensure that the order of banner rotation is random, but every banner gets equal time. All you have to do is to randomize the order of your list of banners when the page loads, and then cycle through them. On each page load, the order is random, but once a page is loaded, the order remains static.</p>
<p>With that in mind, I wrote a JavaScript object to provide the necessary function. I&#8217;ve been writing JavaScript for some time, but I recently bought <a title="JavaScript: The Good Parts" href="http://oreilly.com/catalog/9780596517748" target="_blank">&#8216;JavaScript: The Good Parts&#8217; by <em>Douglas Crockford</em></a>. Crockford has been writing JavaScript for a long time now and has some strong opinions on the language. I for one applaud this book, although I&#8217;m not wholly sold on some of his beliefs. That said, I decided to take a crack at creating an object based on his writings. I&#8217;m not overly convinced that the code I&#8217;ve written is superior to anything else I would have written, but I&#8217;m willing to take the blame for any failings in my code here:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * @author dancrumb
 */</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/*global $ */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> rotatingBanner <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>spec<span style="color: #339933;">,</span> secrets<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 <span style="color: #006600; font-style: italic;">// Private</span>
 <span style="color: #003366; font-weight: bold;">var</span> that <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
 prvItems <span style="color: #339933;">=</span> secrets <span style="color: #339933;">||</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
 prvItems.<span style="color: #660066;">workingList</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
 prvItems.<span style="color: #660066;">cached</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
 prvItems.<span style="color: #660066;">timeoutId</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
 prvItems.<span style="color: #660066;">intialized</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
 prvItems.<span style="color: #660066;">nextBanner</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span> banner <span style="color: #339933;">=</span> prvItems.<span style="color: #660066;">workingList</span>.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 prvItems.<span style="color: #660066;">workingList</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>banner<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000066; font-weight: bold;">return</span> banner<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
 prvItems.<span style="color: #660066;">rotate</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #006600; font-style: italic;">// This should only be executed if initialization is complete</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>prvItems.<span style="color: #660066;">intialized</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #003366; font-weight: bold;">var</span> newBanner <span style="color: #339933;">=</span> prvItems.<span style="color: #660066;">nextBanner</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>spec.<span style="color: #660066;">divId</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' a img'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span>newBanner.<span style="color: #660066;">src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>spec.<span style="color: #660066;">divId</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span>newBanner.<span style="color: #660066;">href</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>spec.<span style="color: #660066;">divId</span><span style="color: #339933;">+</span><span style="color: #3366CC;">' a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'target'</span><span style="color: #339933;">,</span>newBanner.<span style="color: #660066;">target</span> <span style="color: #339933;">||</span> spec.<span style="color: #660066;">defaultTarget</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 prvItems.<span style="color: #660066;">cacheNextImage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000066; font-weight: bold;">return</span> newBanner<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
 prvItems.<span style="color: #660066;">initialize</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span> bannerDiv<span style="color: #339933;">,</span>
 eImg<span style="color: #339933;">,</span>
 eA<span style="color: #339933;">;</span>
 bannerDiv <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span><span style="color: #339933;">+</span>spec.<span style="color: #660066;">divId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>bannerDiv.<span style="color: #660066;">length</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please provide the ID of an element on the page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 bannerDiv.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 eImg <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.
 <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'height'</span><span style="color: #339933;">,</span>spec.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span>.
 <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #339933;">,</span>spec.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span>.
 <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'bannerImage'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 eA <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.
 <span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 eA.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>eImg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 bannerDiv.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>eA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                
&nbsp;
 that.<span style="color: #660066;">randomizeBanners</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 prvItems.<span style="color: #660066;">intialized</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
 prvItems.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #006600; font-style: italic;">/*
 * Place the next Banner Image into the browser cache
 */</span>
 prvItems.<span style="color: #660066;">cacheNextImage</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span> upcomingBanner <span style="color: #339933;">=</span> prvItems.<span style="color: #660066;">workingList</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
 cacheImage<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>prvItems.<span style="color: #660066;">cached</span><span style="color: #009900;">&#91;</span>upcomingBanner.<span style="color: #660066;">src</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 cacheImage <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 cacheImage.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> upcomingBanner.<span style="color: #660066;">src</span><span style="color: #339933;">;</span>
 prvItems.<span style="color: #660066;">cached</span><span style="color: #009900;">&#91;</span>upcomingBanner.<span style="color: #660066;">src</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #006600; font-style: italic;">/*
 * Ensure we, at least, have an array of banners
 */</span>
 <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>
 spec.<span style="color: #660066;">banners</span> <span style="color: #339933;">&amp;&amp;</span>
 <span style="color: #000066; font-weight: bold;">typeof</span> spec.<span style="color: #660066;">banners</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">&amp;&amp;</span>
 <span style="color: #000066; font-weight: bold;">typeof</span> spec.<span style="color: #660066;">banners</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'number'</span> <span style="color: #339933;">&amp;&amp;</span>
 <span style="color: #000066; font-weight: bold;">typeof</span> spec.<span style="color: #660066;">banners</span>.<span style="color: #660066;">splice</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'function'</span> <span style="color: #339933;">&amp;&amp;</span>
 <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>spec.<span style="color: #660066;">banners</span>.<span style="color: #660066;">propertyIsEnumerable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'length'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 spec.<span style="color: #660066;">banners</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #006600; font-style: italic;">/*
 * Confirm that the interval is sensibly set
 */</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> spec.<span style="color: #660066;">interval</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'number'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
 <span style="color: #009900;">&#40;</span>isFinite<span style="color: #009900;">&#40;</span>spec.<span style="color: #660066;">interval</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
 <span style="color: #009900;">&#40;</span>spec.<span style="color: #660066;">interval</span> <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 spec.<span style="color: #660066;">interval</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #006600; font-style: italic;">// Public</span>
 that.<span style="color: #660066;">randomizeBanners</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #006600; font-style: italic;">// We shouldn't fiddle with knownBanners directly as this</span>
 <span style="color: #006600; font-style: italic;">// is our master list of banners</span>
 <span style="color: #003366; font-weight: bold;">var</span> holdingCopy <span style="color: #339933;">=</span> spec.<span style="color: #660066;">banners</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
 index<span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>prvItems.<span style="color: #660066;">timeoutId</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 clearTimeout<span style="color: #009900;">&#40;</span>prvItems.<span style="color: #660066;">timeoutId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 prvItems.<span style="color: #660066;">workingList</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #006600; font-style: italic;">// Randomize the order of the holding copy of known banners and put it</span>
 <span style="color: #006600; font-style: italic;">// into the working list</span>
 <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>holdingCopy.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 index <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span>holdingCopy.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 prvItems.<span style="color: #660066;">workingList</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>holdingCopy<span style="color: #009900;">&#91;</span>index<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 holdingCopy.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>index<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>        
&nbsp;
 <span style="color: #006600; font-style: italic;">// If we cancelled the rotation, we restart it here</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>prvItems.<span style="color: #660066;">timeoutId</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 that.<span style="color: #660066;">startRotation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
 that.<span style="color: #660066;">startRotation</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>
 prvItems.<span style="color: #660066;">timeoutId</span> <span style="color: #339933;">=</span> setInterval<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>
 prvItems.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>spec.<span style="color: #660066;">interval</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
 that.<span style="color: #660066;">addBanner</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>banner<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 spec.<span style="color: #660066;">banners</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>banner<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">randomizeBanners</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
 that.<span style="color: #660066;">setInterval</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>interval<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>prvItems.<span style="color: #660066;">timeoutId</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 clearTimeout<span style="color: #009900;">&#40;</span>prvItems.<span style="color: #660066;">timeoutId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 spec.<span style="color: #660066;">interval</span> <span style="color: #339933;">=</span> interval<span style="color: #339933;">;</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>prvItems.<span style="color: #660066;">timeoutId</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
 that.<span style="color: #660066;">startRotation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#125;</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> prvItems.<span style="color: #660066;">initialize</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: #000066; font-weight: bold;">return</span> that<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can download this script <a href="/js/rotatingBanner.js" alt="Rotating Banners script">here</a></p>
<p>The code above relies on jQuery, so naturally, you would need to include that library first in any HTML page that uses this object.</p>
<p>Invoking this code is straightforward. Your webpage will need a DIV with a defined ID attribute; in this case we use <em>bannerHere</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> rB <span style="color: #339933;">=</span> rotatingBanner<span style="color: #009900;">&#40;</span>
 <span style="color: #009900;">&#123;</span>
 <span style="color: #3366CC;">&quot;width&quot;</span><span style="color: #339933;">:</span>  <span style="color: #CC0000;">600</span><span style="color: #339933;">,</span>
 <span style="color: #3366CC;">&quot;height&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">400</span><span style="color: #339933;">,</span>
 <span style="color: #3366CC;">&quot;banners&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
 <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'fake1'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'fake1'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
 <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'fake2'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;href&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'fake2'</span><span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
 <span style="color: #3366CC;">&quot;interval&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">2000</span><span style="color: #339933;">,</span>
 <span style="color: #3366CC;">&quot;divId&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;bannerHere&quot;</span><span style="color: #339933;">,</span>
 <span style="color: #3366CC;">&quot;defaultTarget&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;blank&quot;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 rB.<span style="color: #660066;">startRotation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><em>width</em> and <em>height</em> are the desired dimensions for the banners<br />
<em>interval</em> is the time (in ms) between each rotation<br />
<em>defaultTarget</em> is the desired value for the TARGET attribute of the banner link<br />
<em>banners</em> is a JavaScript array of JavaScript objects with the following fields:</p>
<p><em>src</em> is the SRC of the banner image<br />
<em>href</em> is the HREF of the banner link<br />
<em>target</em> is the <strong>optional</strong> target for the banner link</p>
<p>The code is based on Crockford&#8217;s belief that we should not pretend the JavaScript is a class-based language. It isn&#8217;t, it&#8217;s a prototype-based language. This means that new objects are created from a &#8216;template&#8217; object or from a factory method, which generates new object. Again, I&#8217;ll repeat that my implementation is quite probably not the best example of this and I&#8217;m not sure that I prefer this code to a class-based approach, but it works and it&#8217;ll do for now.</p>
<p>There are some SEO concerns with this implementation, but I&#8217;m going to save them for an upcoming post. For now, the code about should be enough for you to implement rotating banners on your website</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2009/11/09/rotating-banners-in-random-order/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why web developers should look hard at webOS</title>
		<link>http://www.danrumney.co.uk/2009/02/22/why-web-developers-should-look-hard-at-webos/</link>
		<comments>http://www.danrumney.co.uk/2009/02/22/why-web-developers-should-look-hard-at-webos/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 19:05:21 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[preDevCamp]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[mojo]]></category>
		<category><![CDATA[palm pre]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=136</guid>
		<description><![CDATA[There is a lot of excitement in the mobile development world about the coming of the Palm Pre. Palm&#8217;s been doing pretty poorly of late, so the alternate prospects of abject failure (and bye, bye Palm) and glorious success (bye, bye iPhone) has get everyone all of a quiver. The thing is, I&#8217;m a big [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="Guy At Computer" src="/images/guyAtComputer.jpg" alt="" width="226" height="339" />There is a lot of excitement in the mobile development world about the coming of the Palm Pre. Palm&#8217;s been doing pretty poorly of late, so the alternate prospects of abject failure (and bye, bye Palm) and glorious success (bye, bye iPhone) has get everyone all of a quiver. The thing is, I&#8217;m a big geek&#8230; I&#8217;m just excited about webOS.<br />
<span id="more-136"></span></p>
<p>If you don&#8217;t know, webOS is the new operating system that Palm have created. Instead of having to write apps in Objective C or Java, Palm have opted for having HTML, Javascript and CSS as the language of choice. I think this might be a smart move on their part; the number of applications that developers write is directly proportional to the number of developers who actually <strong>have</strong> the expertise to code for this platform. However, I think Palm have only made <em>half</em> of this smart move.</p>
<p>If you want to change the game, you sometimes need to bring in new players. Right now, the smartphone application development arena is populated by Objectve C developers and Java developers. Many of these developers have been mobile application developers for a long time. They bring great experience with them, but they also bring history. They look at applications in a &#8216;mobile&#8217; way; this is not necessarily the best way. Palm seem to have opened the door, but they&#8217;re not doing anything to point the door out to people or to encourage them to walk through it.</p>
<p>I want to see web developers sit up and take notice of webOS and the Palm Pre. For one thing, it&#8217;s an opportunity for them to branch out and extend their repetoire. They don&#8217;t need to learn a new language; just an alteration in architecture and a new framework. With very little effort, a seasoned web developer can become a webOS developer.<br />
Also, web developers are used to the stateless nature of web pages and have learned to work around this with cookies and HTML 5 Storage functions. In addition, they&#8217;re used to JSON and working with DOM manipulation. That means that they can concentrate on the important aspect of generating innovative ideas for apps, instead of getting to grips with a new language.</p>
<p>Bringing in developers without a traditional mobile mindset will, no doubt, result in some applications that really don&#8217;t perform well on a mobile platform. However, it also brings the potential for some really great ideas, unfettered by the idea of &#8216;what is possible&#8217;. Again, Palm do not appear to be highlighting this; instead they are highlighting the number of PalmOS developers that they have.</p>
<p>The thing is, the number of web developers in the world is staggering. Palm claim 30,000 developers in the Palm development community; contrast this with the over 4.5m downloads of the development version of  jQuery last year alone&#8230; that gives you some kind of indication of how many web developers are out there.</p>
<p>If you&#8217;re a web developer out there, looking for a new challenge, take a long hard look at the Palm Pre. If you&#8217;re interested, go over to the <a href="http://predevcamp.org">preDevCamp website</a> and sign up or organizer a local event; join an evergrowing group of developers excited by this new platform. As a founder of preDevCamp, I have to admit a bias, but I&#8217;m not waiting for Palm to realise what they&#8217;ve got here</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2009/02/22/why-web-developers-should-look-hard-at-webos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
