<?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; Programming</title>
	<atom:link href="http://www.danrumney.co.uk/tag/programming/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>SEO concerns regarding Rotating Banners</title>
		<link>http://www.danrumney.co.uk/2009/11/12/seo-concerns-regarding-rotating-banners/</link>
		<comments>http://www.danrumney.co.uk/2009/11/12/seo-concerns-regarding-rotating-banners/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 19:37:57 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SEO]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=279</guid>
		<description><![CDATA[I recently wrote about a script designed to generate Rotating Banners. The script works fine, but using JavaScript to present a user with links creates a few problems of its own: Google Analytics will not be able to track these external links (if, for instance, you&#8217;re using my Google Analytics for external links) Non-visual User [...]]]></description>
			<content:encoded><![CDATA[<p>I recently wrote about <a href="http://www.danrumney.co.uk/2009/11/09/rotating-banners-in-random-order/">a script designed to generate Rotating Banners</a>. The script works fine, but using JavaScript to present a user with links creates a few problems of its own:</p>
<ul>
<li>Google Analytics will not be able to track these external links (if, for instance, you&#8217;re using my <a href="http://www.danrumney.co.uk/2009/06/21/enabling-external-links-for-google-analytics/">Google Analytics for external links</a>)</li>
<li>Non-visual User Agents will not be able to access these links</li>
<li>As a subset, Google will not be able to crawl these links and so associate your site with the sites those banners point to.</li>
</ul>
<p><span id="more-279"></span><br />
The way to resolve this is to use the method of graceful degradation or, perhaps more appropriately, progressive enhancement. By this, I mean, the HTML should be written such that the above issues are avoided, and then the JavaScript should be written to provide the Rotating Banners functionality.</p>
<p>Using the Rotating Banners script as a starting point, the obvious approach is to dispense with the JSON representation of our banners and replace it with an HTML representation of our banners. This will be hidden using CSS. Much of the functionality will otherwise remain the same. By representing the links in the HTML document, webcrawlers (such as Google&#8217;s indexing service) will be able to parse these links and use them accordingly. In addition, Google Analytics (if installed on your site) can track exits from your site, via these banners.</p>
<p>I have created a <strong>new</strong> object which extends the original Rotating Banners object, such that it doesn&#8217;t cause SEO problems. In order to use this object, you need to include the original object in your HTML, as well as this one.</p>
<p>The new object is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
* SEO Safe Rotating Banner
*
* Specialized version of Rotating Banner
* @author dancrumb
*/</span>
&nbsp;
<span style="color: #009966; font-style: italic;">/*global rotatingBanner,$ */</span> 
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> seoSafeRotatingBanner <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>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</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;
	<span style="color: #003366; font-weight: bold;">var</span> that <span style="color: #339933;">=</span> rotatingBanner<span style="color: #009900;">&#40;</span>spec<span style="color: #339933;">,</span> prvItems<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	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>
&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: #009900;">&#123;</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;
		<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'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> newBanner.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003366; font-weight: bold;">var</span> imgBanner <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;">'img'</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;">'src'</span><span style="color: #339933;">,</span> newBanner.<span style="color: #660066;">src</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: #339933;">;</span>
&nbsp;
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> newBanner.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>imgBanner<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> newBanner.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'inline'</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>
		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;
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div#'</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;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> aElem <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #006600; font-style: italic;">// Ensure that there is an ID defined, for future manipulation</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">var</span> a_id <span style="color: #339933;">=</span> aElem.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</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>a_id <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				a_id <span style="color: #339933;">=</span> <span style="color: #3366CC;">'rb_auto_id_'</span> <span style="color: #339933;">+</span> i<span style="color: #339933;">;</span>
				aElem.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span> a_id<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;">// Ensure that there is a target defined, and set it to the defaultTarget</span>
			<span style="color: #006600; font-style: italic;">// if not</span>
			<span style="color: #003366; font-weight: bold;">var</span> tgt <span style="color: #339933;">=</span> aElem.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'target'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>tgt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				tgt <span style="color: #339933;">=</span> spec.<span style="color: #660066;">defaultTarget</span><span style="color: #339933;">;</span>
				aElem.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'target'</span><span style="color: #339933;">,</span> tgt<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;">// Ensure that the banner is hidden</span>
			aElem.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;">// Add the banner to the rotater</span>
			that.<span style="color: #660066;">addBanner</span><span style="color: #009900;">&#40;</span>
				<span style="color: #009900;">&#123;</span>
					<span style="color: #3366CC;">&quot;src&quot;</span><span style="color: #339933;">:</span> aElem.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
					<span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span> a_id
				<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>
&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;">&#40;</span><span style="color: #3366CC;">'div#'</span> <span style="color: #339933;">+</span> spec.<span style="color: #660066;">divId</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'display'</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;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> that<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This file can be downloaded <a href="/js/seoSafeRotatingBanner.js">here</a>.<br />
All that is necessary is to override the &#8216;initialize&#8217; and &#8216;rotate&#8217; methods.</p>
<p>Using this object on your page is just as straightforward as before. As before, you need to include the jQuery library. You also need to include the original rotatingBanners script, as well as this one. The initialization code is slightly different:</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> seoSafeRotatingBanner<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;interval&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">5000</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>The final ingredient is the list of links itself. This is provided in HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bannerHere&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none;&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;banner1&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;link1&quot;</span>&gt;</span>/image_location/img_src_1.jpg<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;banner2&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;link1&quot;</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;blank&quot;</span>&gt;</span>../img_src_2.png<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;banner3&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;link1&quot;</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;some_target&quot;</span>&gt;</span>img_src3.gif<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Note that the URLs to the images are just plain text. They are <strong>not</strong> <code>IMG</code> tags.</p>
<p>You must ensure that the style of the enclosing DIV contains &#8216;<code>display:none</code>&#8216; (per line 1) or your list of links will be visible to the user.</p>
<p>At this point, you will have your rotating banners, without sacrificing your SEO performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2009/11/12/seo-concerns-regarding-rotating-banners/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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>Peculiar Perl problems&#8230;</title>
		<link>http://www.danrumney.co.uk/2009/07/03/peculiar-perl-problems/</link>
		<comments>http://www.danrumney.co.uk/2009/07/03/peculiar-perl-problems/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 22:20:37 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[errors]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=254</guid>
		<description><![CDATA[I&#8217;ve been coding in Perl for years and, generally, I think I&#8217;m a pretty competent coder. However, every now and again, I fall into a hole that takes me a loooong time to get out of. Invariably, these problems boil down to upload issues, not code issues. As an aide-mémoire for myself and, hopefully, to [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter" title="Perl Camel" src="http://nxadm.files.wordpress.com/2008/05/perl.jpg" alt="" width="298" height="330" /></p>
<p>I&#8217;ve been coding in Perl for years and, generally, I think I&#8217;m a pretty competent coder. However, every now and again, I fall into a hole that takes me a loooong time to get out of. Invariably, these problems boil down to <em>upload</em> issues, not code issues. As an aide-mémoire for myself and, hopefully, to you, I thought I&#8217;d jot down a few problems that I&#8217;ve seen and what the solution was.<br />
<span id="more-254"></span></p>
<h2>Incorrect shebang</h2>
<p>The &#8216;shebang&#8217; is the first line of code in a Perl script. It consists of a hash character &#8216;#&#8217;, followed by an exclamation mark &#8216;!&#8217;, also know as a &#8216;bang&#8217; (hence the name). Then, the full directory of the Perl binary should be given. As often as not, this will be:</p>
<pre>#!/usr/bin/perl</pre>
<p>or</p>
<pre>#!/usr/local/bin/perl</pre>
<p>This is actually a generic line and is not unique to Perl. It&#8217;s a Unix device that tells the operating system that the program that is capable of interpreting the following script can be found here.</p>
<p>Now&#8230; if you get this wrong, it won&#8217;t work. The problem is, if you&#8217;re developing on one box with Perl in one location and you upload your scripts to another box, where Perl is located elsewhere, you&#8217;ll have the phenomenon of your script working perfectly on your development box, but won&#8217;t work on your production box. In addition, if you try to execute the script from the command line of your production box by invoking Perl directly and then provide the script name, that will <em>also</em> work, because the shebang is ignored in this case. It&#8217;s not until you try to execute your script via an HTTP request that it all falls apart. You will see the following in your error log:</p>
<pre>[Fri Jul 03 16:57:14 2009] [error] [client xxx.xxx.xxx.xxx] suexec failure: could not open log file
[Fri Jul 03 16:57:14 2009] [error] [client xxx.xxx.xxx.xxx] fopen: Permission denied
[Fri Jul 03 16:57:14 2009] [error] [client xxx.xxx.xxx.xxx] Premature end of script headers: script.pl</pre>
<p>This should be your indicator to check the shebang in the scripts on your production server and ensure that the point to the correct location for the Perl binary.</p>
<h2>Incorrect file formats</h2>
<p>Sometimes, your Perl script works fine on your Windows development box, but when you transfer it to your Unix production box, it stops working. As above, it works fine when you run it directly from the command line and you invoke &#8216;perl&#8217; directly, but an HTTP request results in a 404 error, even though the file <em>is</em> actually in place.</p>
<p>The problem here is that DOS and Unix use different characters to indicate the end of a line and, if you have DOS endings in a Unix environment, the O/S is not able to parse the shebang correctly and so cannot locate the Perl binary. The way to fix this is to use the &#8216;dos2unix&#8217; command. This will convery a DOS encoded file into a Unix encoded file and everything will be peachy.</p>
<p>This normally occurs if you&#8217;re using an FTP client that is uploading Perl files in BINary mode instead of ASCII mode. It also occurs if you are transferring files via SCP, since SCP does not convert files as it copies them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2009/07/03/peculiar-perl-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What have we learned about webOS?</title>
		<link>http://www.danrumney.co.uk/2009/02/18/what-have-we-learned-about-webos/</link>
		<comments>http://www.danrumney.co.uk/2009/02/18/what-have-we-learned-about-webos/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 07:17:33 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[preDevCamp]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[mojo]]></category>
		<category><![CDATA[pre]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=124</guid>
		<description><![CDATA[O&#8217;reilly release the first chapter of their new webOS book in the past few days and it has been a welcome island in a sea of tedious rumours. While others may wildly speculate on when the Pre will appear, at least we now have some idea of what it will be running. So what does [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" title="webOS Rough Cuts" src="/images/webOSRoughCutsTitle.png" alt="" width="410" height="224" /></p>
<p>O&#8217;reilly release <a href="http://oreilly.com/catalog/9780596801816/">the first chapter of their new webOS book</a> in the past few days and it has been a welcome island in a sea of tedious rumours. While others may wildly speculate on <strong>when</strong> the Pre will appear, at least we now have some idea of what it will be running.<br />
<span id="more-124"></span></p>
<p>So what does this chapter tell us about webOS? Well, a lot of the chapter is spent with the usual overview. I tend to skip these chapters in technical books, but webOS is brand new to me, so I gave it the time. Here&#8217;s what I learned:</p>
<ul>
<li>webOS is expressly intended to support multiple hardware environments with all sorts over screen sizes, resolutions and orientations &#8211; This tells me that a good webOS App will not restrict itself to functioning on a Pre form factor</li>
<li>webOS will use HTML 5 tags to provide storage functions and some graphic functions (using the canvas) &#8211; This tells me that I need to get up to speed on HTML 5 and start playing with it in Firefox</li>
<li>webOS will give you access to Calendar and Contact data, Location Services, Accelerometer data and XMPP messaging  &#8211; This tells me that the apps we can make are going to be awwwwesome!</li>
<li>Mojo will come bundled with the <a href="http://www.prototypejs.org/">Prototype</a> framework &#8211; Since I&#8217;m a jQuery fan, this means that I have to learn <strong>another </strong>JS framework, if I&#8217;m going to be able to fully exploit the Pre</li>
<li>The Core OS will support fat32 for media file partitions (with a mention of mounting via USB) &#8211; This suggests that it may be possible to plug a USB drive into the Pre for extra storage and ease of file transfer!</li>
</ul>
<p>There are some  nice pieces about design philosophy and UI considerations in the first chapter and some high level introduction to concepts such as <em>stages</em> and <em>scenes</em>.</p>
<p>All in all, and interesting read; certainly enough to whet my appetite for the Pre. Here&#8217;s what I&#8217;m going to be doing in preparation:</p>
<ol>
<li>Learn HTML 5</li>
<li>Learn Prototype</li>
</ol>
<p>I think focusing on those two, for now, will put me in good stead. I recommend the same to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2009/02/18/what-have-we-learned-about-webos/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A History of Languages</title>
		<link>http://www.danrumney.co.uk/2009/02/15/a-history-of-languages/</link>
		<comments>http://www.danrumney.co.uk/2009/02/15/a-history-of-languages/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 01:30:22 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[preDevCamp]]></category>
		<category><![CDATA[assembler]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[korn shell]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=104</guid>
		<description><![CDATA[I saw @mhat&#8216;s tweet today about &#8216;What language should I learn&#8216;  and it got me thinking about the times I&#8217;ve asked that question and how I might answer it. So, @mhat, I&#8217;m taking your tweet to be a challenge to write an unbiased and &#8216;unwrong&#8217; post. To do this, I&#8217;m going to give you a [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" title="Laptop and Books" src="/images/laptopAndBooks.jpg" alt="" width="425" height="282" /></p>
<p>I saw <a href="http://twitter.com/mhat">@mhat</a>&#8216;s tweet today about &#8216;<a href="http://twitter.com/mhat/status/1212746649">What language should I learn</a>&#8216;  and it got me thinking about the times I&#8217;ve asked that question and how I might answer it. So, @mhat, I&#8217;m taking your tweet to be a challenge to write an unbiased and &#8216;unwrong&#8217; post. To do this, I&#8217;m going to give you a history of the languages I have learned.<br />
<span id="more-104"></span></p>
<h2>The early years</h2>
<p>As I wrote in <a href="http://www.danrumney.co.uk/2009/01/25/all-geekness-great-and-small/">my first post</a>, I started at a tender age, on a ZX Spectrum. The Spectrum came with a dialect of BASIC, called &#8216;Sinclair Basic&#8217;. Like all <a href="http://en.wikipedia.org/wiki/BASIC_programming_language">BASIC</a> derivatives, it&#8217;s a high level language, however unlike modern flavours of BASIC, it was untyped and unstructured. Lines of code were entered with the line number provided explicitly. There was no variable scoping and subroutines were implemented as a special &#8216;GOTO&#8217; called &#8216;GOSUB&#8217; which allowed you to &#8216;RETURN&#8217; to the next line. However, since there was no variable scoping, recursion was impossible.</p>
<p>That said, I was 5 at the time, I wasn&#8217;t too worried about recursion at the time. My programming was limited to copying code out of books containing page after page of ZX Spectrum programs. My brother and I would diligently type these out, then play the game for a while and then start playing with the code to see what changes we could make. This process was about as successful as genetic mutation. More often than not, our change would kill the program, but sometimes it would lead to great stuff.</p>
<p>As we got older, our 48K Spectrum was replaced with a 128K Spectrum. Along with the extra memory, came an update to BASIC to include keywords that allowed direct access to the MIDI chip. I continued to mess around with BASIC, making programs that would play both hands of a piano score, as well as simple games.</p>
<h2>The IBM PC arrives</h2>
<p>My first experience with an IBM PC was the 286. I migrated from Sinclair BASIC to <a href="http://en.wikipedia.org/wiki/Microsoft_GW-BASIC_interpreter">GW-BASIC</a>. At this point, I was writing programs from scratch, including a text adventure called &#8216;Loup-Garou Chateau&#8217;. Tragically, this game has been lost from prosperity, but it was my first foray into actually <em>designing</em> a program before coding it. I chose GW-BASIC, simply because I was very familiar with BASIC and it made for the simplest transition.</p>
<p>As the years passed, I would inherit my dad&#8217;s old computers, moving up through the 386 to the 486. At this point, I got my hands on <a href="http://en.wikipedia.org/wiki/Visual_Basic">Visual Basic</a>. Again, this transition was driven by the ease of transition from what I knew to something new. This was my first experience with a proper development IDE, as well as my first experience with creating GUIs. Beyond that, it wasn&#8217;t a great step forward to me&#8230; that would come during university.</p>
<h2>Coding gets hardcore</h2>
<p>I never studied computing at university; I was a physics student. However, the course I took, included some small computing components. For one lab rotation, we learned how to use computers for physical analysis. This was back in 2000 and they had us coding with <a href="http://en.wikipedia.org/wiki/Pascal_(programming_language)">Pascal</a>. At the same time, I was playing with <a href="http://en.wikipedia.org/wiki/C_(programming_language)">C</a> at the suggestion of my dad. For the first time, I was learning about functions and variable typing. In addition, I was programming with the intention to solve problems that were being presented to me, instead of just &#8216;playing&#8217; with a language and seeing what it could do. When you have an externally provided problem to solve, you&#8217;re forced to learn how to use new facets of the language. If you&#8217;re just playing around, you can be limited to seeing what you can do with the parts of the language you understand.</p>
<p>While at university, I also participated in an Electronics lab which involved learning about the 8086 processor. We had an experimental board which allowed us to commit opcodes to a region of memory and then execute them. As part of the practical, we actually had to wire up the SUB instruction, by running the clock signal through the appropriate logic gates and to the appropriate ALU pins. This was my first experience with the lowest level of programming.</p>
<p>In the summer between my second and third years, I spent four weeks working for <a href="http://www.milinst.com/">Milford Instruments</a>. While there, I developed a <a href="http://www.ppmilinst.redcetera.com/shop/DMX/pdf/1_463.pdf">DMX-512 Transceiver Controller</a>. I was working with the SX-Chip from Parallax and learned how to write assembler, how to work with interrupt handlers and how to get microcontrollers to talk to other microchips. For 4 weeks, I worked on the firmware and the hardware and really learned what programming was all about&#8230;</p>
<p><img class="alignnone" title="Computer Code" src="/images/computerCode.jpg" alt="" width="400" height="300" /></p>
<h2>Coding starts to bring in money</h2>
<p>My month at Milford Instruments made me realise what I wanted to do with my time; I wanted to get into development professionally. It was an obvious choice to apply to IBM and I started working at their Hursley Development Lab, one weeks after school ended. They had me working on a protocol converter, (this time, a SCSI-SSA converter) and, as before, it was coded in assembler.</p>
<p>After a couple of years working on this, I moved to work on one of their Storage RAID controllers. This time I was coding in C, the first time I had done so, professionally. However, alongside all this production code, I was steadily learning how to write <a href="http://en.wikipedia.org/wiki/Perl">Perl</a> and <a href="http://en.wikipedia.org/wiki/Korn_shell">Korn Shell</a> scripts for developing test rigs. I was also teaching myself how to code for the web. I&#8217;d done some basic page development while at university, but I was beginning to learn about dynamic pages and DOM manipulation.</p>
<p>My next move got me into support and I started writing a web-based support tool. It uses Perl and <a href="http://en.wikipedia.org/wiki/MySQL">MySQL</a> in the back-end and HTML and Javascript in the front-end.</p>
<p>The main thing I got from the IBM Development Lab was the chance to be surrounded by people writing a wide variety of code to solve a wide variety of problems and be exposed to what other people were doing with languages that I both knew and that I had never heard of. I learned that there is really no such thing as the one perfect language, rather the best language for the job in hand.</p>
<h2>What&#8217;s the point of all this?</h2>
<p>So, what&#8217;s the point of this post? Is it possible to write a post about &#8220;Which language should I use&#8221; that is not biased and has some value? I hope so. Here&#8217;s what I feel I&#8217;ve learned over the years.</p>
<p>People write code for a wide variety of reasons. The language that they code in is also determined by a multitude of factors. More often than not, we don&#8217;t have a truly free hand when it comes to choosing the language we&#8217;re coding in. However, looking back, I believe I see some benefits in the track I took.</p>
<ul>
<li>Learning C allows you to truly understand what higher level languages such as Perl or Java are doing with objects and references. Although people get scared off by pointers, if you don&#8217;t understand the concept, then you&#8217;re going to struggle with the idea of references. Since all languages have variables, if you can get those nailed and totally understood, you&#8217;re going to be in good stead when you move to a language which brings new data types</li>
<li>Learning Assembler allows you to truly understand what C is doing with pointers. Assembler has a tiny command set and you have to tell it to do every last thing. It&#8217;s a struggle, but once you&#8217;ve got assembler in your tool belt you&#8217;re in good shape. You may never go back to it, but the lessons you learn from it can be applied all the way up the language stack</li>
<li>Learning a wide variety of languages allows you to truly understand the patterns of development that are language-independent and stops you falling into the trap of becoming a language bigot. There&#8217;s nothing wrong with having a favourite language, but saying that one language is categorically better than another because of the presence of a specific feature is like praising Inuit for its wide variety of words for snow. That&#8217;s fine if you&#8217;re describing snow, but not much use if you want to discuss the surfing conditions in Hawaii.</li>
<li>Learning a language is a lot easier if you&#8217;re trying to solve a specific problem that has come from an external source. This is why it&#8217;s such a good idea to do the exercises if you&#8217;re learning from a textbook. There&#8217;s nothing wrong with playing with a feature for its own sake, but unless you have some factor that is external to the language driving you, it&#8217;s going to be tough to learn all that the language can do.</li>
</ul>
<h2>Did I just cop out?</h2>
<p>So, did I just cop out of the question? If someone asked me what language they should learn, how would I answer? That depends on the person. If they were just starting out, I would say Perl or Python. These languages have a huge development community and millions of pages dedicated to coding in them. They are in common usage and show no signs of disappearing in the near future. They are neither arcane nor limited in their range and they allow for procedural and object oriented style programming, meaning that the developer can transition to OO without having to change languages.</p>
<p>If I was being asked by someone with a reasonable amount of development, I would strongly suggest Javascript. As the powerhouse behind Web 2.0 and the basis for coding on the Palm Pre, it&#8217;s critical to know this language. However, testing and debugging Javascript is not a trivial task and requires a developer who is comfortable writing code that has &#8216;a good chance&#8217; of working so that troubleshooting is limited to ironing out wrinkle. Also, the problems of cross-browser support are bewildering enough for experienced coders; exposing a brand new coder to Javascript would probably be the end of their efforts in the field.</p>
<p>If someone wanted to spread their wings a little, I&#8217;d strongly suggest taking a look at Assembler or some other microprocessor based language. It&#8217;s still coding, but you&#8217;re forced to truly understand your code&#8217;s interaction with external factors that don&#8217;t care about your nice neat program. Spending time debugging microprocessor code; especially code that interacts with an external piece of hardware, gives you a true understanding of the many ways that code can fail to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2009/02/15/a-history-of-languages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
