<?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</title>
	<atom:link href="http://www.danrumney.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.danrumney.co.uk</link>
	<description>Technology from work and home</description>
	<lastBuildDate>Wed, 09 May 2012 13:20:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Figuring out Method Resolution Order in Dojo</title>
		<link>http://www.danrumney.co.uk/2012/05/08/figuring-out-method-resolution-order-in-dojo/</link>
		<comments>http://www.danrumney.co.uk/2012/05/08/figuring-out-method-resolution-order-in-dojo/#comments</comments>
		<pubDate>Wed, 09 May 2012 04:22:07 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[mro]]></category>
		<category><![CDATA[utility]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=526</guid>
		<description><![CDATA[Dojo allows the simulation of class-based inheritance with the declare function. It even allows for multiple inheritance. However, this raises questions of how to figure out which Function to call, when a method of an instantiated Object is provided by a parent object. This article provides guidance on how to figure it out and a [...]]]></description>
			<content:encoded><![CDATA[<p>Dojo allows the simulation of class-based inheritance with the <a href="http://dojotoolkit.org/reference-guide/1.7/dojo/declare.html">declare</a> function. It even allows for multiple inheritance. However, this raises questions of how to figure out which Function to call, when a method of an instantiated Object is provided by a parent object. This article provides guidance on how to figure it out and a handy little function to determine <abbr title="Method Resolution Order">MRO</abbr> for certain.</p>

<p><span id="more-526"></span></p>

<h1>Method Resolution Order</h1>

<p>When you have an Object that inherits methods from multiple parent Object, there needs to be a definitive way to determine which of the parent Objects implements methods not directly implemented in the child object. Dojo uses the <a href="http://www.python.org/download/releases/2.3/mro/">C3 Method Resolution Order</a> and you can read all the details by following that link. However, it gets a little technical, so I&#8217;ll try to summarize here.</p>

<p>Normally, it&#8217;s pretty easy to figure things out. Consider the following:</p>

<p><img src="/images/mro/simple_inheritance.jpg" alt="Basic Inheritance Chain" /></p>

<p>It&#8217;s easy to figure out which Function object to use for the method call <code>C.method()</code>. If it&#8217;s not in <code>C</code>, look in <code>B</code>. If it&#8217;s not there, look in <code>A</code>.</p>

<p>Now consider this diagram, which shows the well-known <a href="http://en.wikipedia.org/wiki/Diamond_problem">Diamond problem</a></p>

<p><img src="/images/mro/diamond.jpg" alt="Diamond problem diagram" /></p>

<p>In this diagram, <code>C</code> inherits from <code>B</code> and <code>D</code>, which both inherit from <code>A</code>. So here&#8217;s the question: if <code>C</code> overrides a method from <code>A</code> called <code>foo</code>, and <code>B</code> and <code>D</code> both override that method differently, which Function will be used for the method call <code>C.foo()</code>?</p>

<p>That&#8217;s where <abbr title="Method Resolution Order">MRO</abbr> comes in. <abbr title="Method Resolution Order">MRO</abbr> is used to determine which order to search for a method in a network of classes. The <a href="http://www.python.org/download/releases/2.3/mro/">C3 Method Resolution Order</a> guarantees that this will be predictable.</p>

<h1>Figuring out <abbr title="Method Resolution Order">MRO</abbr> for your classes</h1>

<p>Let&#8217;s say you&#8217;ve declare the following classes:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">require<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'dojo/_base/declare'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>declare<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'A'</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;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'B'</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;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'C'</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;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'D'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>A<span style="color: #339933;">,</span>B<span style="color: #339933;">,</span>C<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'E'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>C<span style="color: #339933;">,</span>D<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>If you want to know which order E will use to search for a method, you could hand crunch the <abbr title="Method Resolution Order">MRO</abbr> yourself. If you don&#8217;t want to, then the following function will help you out:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">define<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mro'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'dojo/_base/array'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>arrayUtils<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;">function</span><span style="color: #009900;">&#40;</span>proto<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> arrayUtils.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>proto._meta.<span style="color: #660066;">bases</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>base<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
      <span style="color: #000066; font-weight: bold;">return</span> base.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">declaredClass</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; -&gt; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>With this function: <code>mro</code>, you can take the constructor of any Object and determine its <abbr title="Method Resolution Order">MRO</abbr>, like so:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">require<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'dojo/_base/declare'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'mro'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>declare<span style="color: #339933;">,</span> mro<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'A'</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;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'B'</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;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'C'</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;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'D'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>A<span style="color: #339933;">,</span>B<span style="color: #339933;">,</span>C<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'E'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>C<span style="color: #339933;">,</span>D<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>mro<span style="color: #009900;">&#40;</span>E<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>


<p>This will display <code>E -&gt; D -&gt; C -&gt; B -&gt; A</code> in an alert box. This shows you the order of Objects that will be search in order to resolve a method call.</p>

<p>If you&#8217;d like to play with this, you can do so in a <a href="http://dojo-sandbox.net/public/d4190/0">Dojo sandbox</a>. And that&#8217;s all you need to know. If you&#8217;re interested in why I wrote this, you can read on, but that&#8217;s all you need in order to starting checking MROs for your Objects.</p>

<h1>Why did I need to know about <abbr title="Method Resolution Order">MRO</abbr>?</h1>

<p>If you&#8217;ve ever used the <a href="http://livedocs.dojotoolkit.org/dijit/Toolbar">Dijit Toolbar</a>, you may know that it captures the left and right arrow keys in order to move focus between buttons. Unfortunately, if you put a text box into your toolbar, you can no longer move the caret, as the arrow keys just shit focus instead of shifting the cursor.</p>

<p>It does this with the <code>_KeyNavContainer</code> mixin:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">define<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>
    <span style="color: #3366CC;">&quot;require&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;dojo/_base/declare&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// declare</span>
        ...
    <span style="color: #3366CC;">&quot;./_Widget&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;./_KeyNavContainer&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;./_TemplatedMixin&quot;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>require<span style="color: #339933;">,</span> declare<span style="color: #339933;">,</span> ... <span style="color: #339933;">,</span> _Widget<span style="color: #339933;">,</span> _KeyNavContainer<span style="color: #339933;">,</span> _TemplatedMixin<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    ...
    <span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dijit.Toolbar&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>_Widget<span style="color: #339933;">,</span> _TemplatedMixin<span style="color: #339933;">,</span> _KeyNavContainer<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>


<p>In order to override this behaviour, I created my own <code>Toolbar</code> and <code>_KeyNavContainer</code>:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">define<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;dojo/_base/declare&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// declare</span>
        ...
    <span style="color: #3366CC;">&quot;dijit/_KeyNavContainer&quot;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>declare<span style="color: #339933;">,</span> ... <span style="color: #339933;">,</span>_KeyNavContainer<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;danrumney._KeyNavContainer&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>_KeyNavContainer<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
&nbsp;
...
&nbsp;
<span style="color: #660066;">define</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;dojo/_base/declare&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// declare</span>
        ...
    <span style="color: #3366CC;">&quot;dijit/Toolbar&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #3366CC;">&quot;danrumney/_KeyNavContainer&quot;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>declare<span style="color: #339933;">,</span> ... <span style="color: #339933;">,</span>_KeyNavContainer<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;danrumney.Toolbar&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>Toolbar<span style="color: #339933;">,</span> _KeyNavContainer<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>


<p>In order to confirm that my overriding Objects would be the ones to provide Functions for method calls against <code>danrumney.Toolbar</code>, I wrote the <abbr title="Method Resolution Order">MRO</abbr> code:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">define<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mro'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'dojo/_base/array'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>arrayUtils<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;">function</span><span style="color: #009900;">&#40;</span>proto<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> arrayUtils.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>proto._meta.<span style="color: #660066;">bases</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>base<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
      <span style="color: #000066; font-weight: bold;">return</span> base.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">declaredClass</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; -&gt; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
require<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'dojo/_base/declare'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'mro'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>declare<span style="color: #339933;">,</span> mro<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dijit_Widget'</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;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dijit_TemplatedMixin'</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;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dijit_KeyNavContainer'</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;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dijit_Toolbar'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>dijit_Widget<span style="color: #339933;">,</span> dijit_TemplatedMixin<span style="color: #339933;">,</span> dijit_KeyNavContainer <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dcr_KeyNavContainer'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>dijit_KeyNavContainer <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dcr_Toolbar'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>dijit_Toolbar<span style="color: #339933;">,</span>dcr_KeyNavContainer<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>mro<span style="color: #009900;">&#40;</span>dcr_Toolbar<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>


<p>This gives</p>

<pre><code>dcr_Toolbar -&gt; dcr_KeyNavContainer -&gt; dijit_Toolbar 
            -&gt; dijit_KeyNavContainer -&gt; dijit_TemplatedMixin 
            -&gt; dijit_Widget
</code></pre>

<p>which is exactly what I wanted. Hooray for <abbr title="Method Resolution Order">MRO</abbr>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2012/05/08/figuring-out-method-resolution-order-in-dojo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problems with Event handling and AOP advice in Dojo when hitch is in play</title>
		<link>http://www.danrumney.co.uk/2012/05/06/problems-with-event-handling-and-aop-advice-in-dojo-when-hitch-is-in-play/</link>
		<comments>http://www.danrumney.co.uk/2012/05/06/problems-with-event-handling-and-aop-advice-in-dojo-when-hitch-is-in-play/#comments</comments>
		<pubDate>Mon, 07 May 2012 01:19:18 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Dojo]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[aspect]]></category>
		<category><![CDATA[connect]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[hitch]]></category>
		<category><![CDATA[problems]]></category>
		<category><![CDATA[quirks]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=486</guid>
		<description><![CDATA[An analysis of problems created when using Dojo's hitch, connect and aspect methods in a complex web application.]]></description>
			<content:encoded><![CDATA[<p>Anyone who has spent time developing web-apps with Dojo will have found the need to associate their own functionality with the raising of events and calling of methods on other objects that may not be within their control. Dojo allows developers to attach <em>Listeners</em> (callback methods) to <em>Actions</em> (DOM events or object method calls) in order to allow such wiring.</p>

<p>Some of you may have stumbled across an issue whereby a Listener has been attached to an Action, but isn&#8217;t being called when that Action executes. This article explains what&#8217;s going on an how to avoid it.</p>

<p><span id="more-486"></span></p>

<blockquote>
  Throughout this article, you&#8217;ll see me using the <abbr title="Asynchronous Module Definition">AMD</abbr> style of module use. That is, I won&#8217;t be referring to <code>dojo</code>.methods. If you&#8217;re not familiar with Dojo&#8217;s <abbr title="Asynchronous Module Definition">AMD</abbr>, take a look at this article about <a href="http://dojotoolkit.org/reference-guide/1.7/loader/amd.html">the Dojo Loader</a> at some point. Until then, don&#8217;t worry: all this affects for this article is what the method calls look like; the internals are the same.
</blockquote>

<h1>The problem</h1>

<p>Take a look at this code and see if you can figure out what the output will be:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*jshint dojo:true devel:true strict:false*/</span>
require<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'dojo/_base/declare'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'dojo/_base/lang'</span><span style="color: #339933;">,</span>
         <span style="color: #3366CC;">'dojo/_base/connect'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> 
    <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>declare<span style="color: #339933;">,</span> lang<span style="color: #339933;">,</span>
             connect<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Foo&quot;</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;">&#123;</span>
        <span style="color: #009966; font-style: italic;">/*global Foo*/</span>
            constructor <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&lt;undef&gt;&quot;</span><span style="color: #339933;">;</span>
               console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Instantiated a Foo called '&quot;</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            aMethod<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>
                console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;.aMethod called with arguments: %o&quot;</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            secondMethod<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>
                console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;.secondMethod called with arguments: %o&quot;</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        declare<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Bar&quot;</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;">&#123;</span>
            <span style="color: #009966; font-style: italic;">/*global Bar */</span>
           constructor <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">=</span> <span style="color: #000066;">name</span> <span style="color: #339933;">||</span> <span style="color: #3366CC;">&quot;&lt;undef&gt;&quot;</span><span style="color: #339933;">;</span>
               console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Instantiated a Bar called '&quot;</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
           anotherMethod<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>
                console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;.anotherMethod called with arguments: %o&quot;</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Calling foo.aMethod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        foo.<span style="color: #660066;">aMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;After init&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> bar <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Bar<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'bar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Calling bar.anotherMethod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        bar.<span style="color: #660066;">anotherMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;After init&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Getting a reference to bar.anotherMethod called barAM&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> barAM <span style="color: #339933;">=</span> lang.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span>bar<span style="color: #339933;">,</span> bar.<span style="color: #660066;">anotherMethod</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Connecting foo.aMethod to bar.anotherMethod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        connect.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>bar<span style="color: #339933;">,</span><span style="color: #3366CC;">'anotherMethod'</span><span style="color: #339933;">,</span> foo<span style="color: #339933;">,</span> foo.<span style="color: #660066;">aMethod</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Calling bar.anotherMethod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        bar.<span style="color: #660066;">anotherMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Direct call'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Calling barAM&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        barAM<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Via barAM'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Getting another reference to bar.anotherMethod called barAM2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> barAM2 <span style="color: #339933;">=</span> lang.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span>bar<span style="color: #339933;">,</span> bar.<span style="color: #660066;">anotherMethod</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Connecting foo.secondMethod to bar.anotherMethod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        connect.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>bar<span style="color: #339933;">,</span><span style="color: #3366CC;">'anotherMethod'</span><span style="color: #339933;">,</span> foo<span style="color: #339933;">,</span> foo.<span style="color: #660066;">secondMethod</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Calling bar.anotherMethod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        bar.<span style="color: #660066;">anotherMethod</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Direct call'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Calling barAM&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        barAM<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Via barAM'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        console.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Calling barAM2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        barAM2<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Via barAM2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>Here&#8217;s what you will see in your console:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="none" style="font-family:monospace;">Instantiated a Foo called 'foo'
Calling foo.aMethod
foo.aMethod called with arguments: [&quot;After init&quot;]
&nbsp;
Instantiated a Bar called 'bar'
Calling bar.anotherMethod
bar.anotherMethod called with arguments: [&quot;After init&quot;]
&nbsp;
Getting a reference to bar.anotherMethod called barAM
Connecting foo.aMethod to bar.anotherMethod
Calling bar.anotherMethod
bar.anotherMethod called with arguments: [&quot;Direct call&quot;]
foo.aMethod called with arguments: [&quot;Direct call&quot;]
&nbsp;
Calling barAM
bar.anotherMethod called with arguments: [&quot;Via barAM&quot;]
&nbsp;
Getting another reference to bar.anotherMethod called barAM2
Connecting foo.secondMethod to bar.anotherMethod
Calling bar.anotherMethod
bar.anotherMethod called with arguments: [&quot;Direct call&quot;]
foo.aMethod called with arguments: [&quot;Direct call&quot;]
foo.secondMethod called with arguments: [&quot;Direct call&quot;]
&nbsp;
Calling barAM
bar.anotherMethod called with arguments: [&quot;Via barAM&quot;]
&nbsp;
Calling barAM2
bar.anotherMethod called with arguments: [&quot;Via barAM2&quot;]
foo.aMethod called with arguments: [&quot;Via barAM2&quot;]
foo.secondMethod called with arguments: [&quot;Via barAM2&quot;]</pre></td></tr></table></div>


<p>First, we instantiate two objects: <code>foo</code> and <code>bar</code> and we call some methods to demonstrate how those methods behave.
Then, we get a reference to the <code>bar.anotherMethod</code> called <code>barAM</code>. We connect <code>foo.aMethod</code> to <code>bar.anotherMethod</code>, so that <code>foo.aMethod</code> is called whenever <code>bar.anotherMethod</code> is called.
Next, we call <code>bar.anotherMethod</code> and, as expected, we see that <code>foo.aMethod</code> is called.
After that, we call <code>barAM</code>, but we see that <code>foo.aMethod</code> is <strong>not</strong> called, even though <code>bar.anotherMethod</code> <strong>was</strong>.
OK, so maybe it&#8217;s because we created <code>barAM</code> <em>before</em> we connected <code>foo.aMethod</code>. So, we create another reference to <code>bar.anotherMethod</code> and call it <code>barAM2</code>.
We also connect another method (<code>foo.secondMethod</code>) to <code>bar.anotherMethod</code>.
This time, when we call <code>bar.anotherMethod</code> we see both of our connected methods also being called. Great; this is what we expect.
Then, we call <code>barAM</code> and we see that <strong>only</strong> <code>bar.anotherMethod</code> is called. Not what we want, but what we now expect.
Finally, we call <code>barAM2</code>. This time, we see that <strong>both</strong> <code>foo</code> methods are called, even though we attached <code>secondMethod</code> <em>after</em> we took our reference.</p>

<p>What&#8217;s going on?</p>

<p>First, let&#8217;s look at the wiring&#8230;</p>

<h1>The wiring</h1>

<p>Prior to Dojo 1.7, this was all handled by the <a href="http://livedocs.dojotoolkit.org/dojo/connect">connect</a> module. Since the advent of Dojo 1.7, this functionality has been split out into the [<code>on</code>][] module for event-handling and <a href="http://livedocs.dojotoolkit.org/dojo/aspect">aspect</a> module for Aspect Oriented Programming (AOP) advice giving. Indeed, the 1.7 version of <code>connect.connect()</code> is essentially a wrapper around calls to <code>on()</code> and <code>aspect.after()</code>. First, let&#8217;s look at <code>connect</code></p>

<h2>Dojo connect</h2>

<p>If you take a look at the <a href="http://dojotoolkit.org/api/dojo#dojo.connect">connect API</a>, you&#8217;ll see the method signature:</p>

<pre><code>connect.connect(obj, event, context, method, dontFix);
</code></pre>

<dl>
<dt><code>obj</code></dt>
<dd>This is the object that fires the Action. It might be a DOM node or just a plain old object. It can be omitted, but we&#8217;re not going to worry about  that here.</dd>

<dt><code>event</code></dt>
<dd>This is the Action that was mentioned above. It might be an event; it might be a method name. Either way, it&#8217;s just a String that represents the Action that we&#8217;re interested in connecting to</dd>

<dt><code>context</code></dt>
<dd>This is the context in which the <code>method</code> will be called. Basically, the context of a method is the value that is returned if the method refers to <code>this</code>. If no context is given to <code>connect.connect()</code>, then the context will be set to the event&#8217;s context.</dd>

<dt><code>method</code></dt>
<dd>This is the function that will be called after the Action takes place. This can be a Function object (i.e. a reference to a function) or it can be a String. If it is a String, then the function that is called is <code>context[method]</code>.</dd>

<dt><code>dontFix</code></dt>
<dd>Don&#8217;t worry about this. It&#8217;s not relevant to this discussion.</dd>
</dl>

<p>In Dojo 1.7, <code>connect.connect()</code> follow this recipe:</p>

<ol>
<li>Normalises the input parameters, to handle any that were set to <code>null</code> or left undefined</li>
<li><strong>Hitch the <code>method</code> to the <code>context</code></strong></li>
<li>If <code>obj</code> is not a DOM node, then use <code>aspect.after()</code> to attach the Listener to the Action</li>
<li>If <code>obj</code> <em>is</em> a DOM node, do some clean up for the <code>event</code> to ensure that cross-browser support works and then use <code>on()</code> to attach the Listener to the Action</li>
</ol>

<p>If you&#8217;ve already moved away from using <code>connect.connect()</code>, you&#8217;re probably using <code>lang.hitch()</code> yourself, to ensure that your Listener is called in the context of its providing object. Here&#8217;s where you can get into problems. To understand why, though, lets look at how <code>aspect.*</code> works.</p>

<h2>Dojo aspect</h2>

<p>The Dojo <a href="http://dojotoolkit.org/api/dojo/aspect">aspect API</a> defines three functions: <code>before</code>, <code>after</code> and <code>around</code>. They have very similar signatures:</p>

<pre><code>aspect.before(target,methodName,advice)
aspect.around(target,methodName,advice)
aspect.after(target,methodName,advice,receiveArguments)
</code></pre>

<dl>
<dt><code>target</code></dt>
<dd>This is the object that is being targeted with the advice.</dd>

<dt><code>methodName</code></dt>
<dd>This is the method of the <code>target</code> that is being targeted with the advice.</dd>

<dt><code>advice</code></dt>
<dd>This is the function to be called at a time indicated by the <code>aspect.*</code> method. More often than not, this will be a method that has been hitched to another object.</dd>

<dt><code>receiveArguments</code></dt>
<dd>This just controls whether the <code>after</code> advice receives the arguments that were passed to <code>methodName</code></dd>
</dl>

<p>The way that Dojo handles this is to create a dispatch Function that creates a linked list of Function objects. The <code>before</code> advice comes first, then the targeted method, then the <code>after</code> advice. A little more work is needed for <code>around</code> advice, but the principle remains the same.</p>

<h1>How it all works</h1>

<p>In Javascript, an Object is simply an <a href="http://en.wikipedia.org/wiki/Associative_array">associative array</a> with some special features. The keys of the Object are the field names and method names. The values of the Object are the field values and Function objects. In the image below, you can see a representation of the two objects created in our code sample:</p>

<p><img src="/images/dojo-problem/basic_objects.jpg" alt="Basic object diagram" /></p>

<p>You can see the Function objects represented as rounded rectangles. Plain rectangles represent variable names.
Whenever a call is made to <code>bar.anotherMethod</code>, the <code>anotherMethod</code> Function object is called, with <code>bar</code> as the context.</p>

<p>In the next diagram, we create our reference to <code>bar.anotherMethod</code>. When this happens, Javascript looks to see what <code>bar.anotherMethod</code> points to and points <code>barAM</code> to that. The little <code>bar</code> on the arrow tells us that this is a hitch and that the context for this call will be <code>bar</code>.
At this point, you can see that a call to <code>bar.anotherMethod</code> and <code>barAM</code> will be identical. They point to the same Function object and will be called with the same context.</p>

<p><img src="/images/dojo-problem/reference.jpg" alt="Initial hitch" /></p>

<p>Next, we perform our <code>connect</code>, which is just an <code>aspect.after</code>. Here&#8217;s where the problem kicks in.</p>

<p><img src="/images/dojo-problem/connect.jpg" alt="Initial connect" /></p>

<p>When the <code>aspect.after</code> is performed, Dojo replaces the <code>anotherMethod</code> Function with the <code>DISPATCHER</code> function that handles all of the AOP magic. Now, <code>bar.anotherMethod</code> points to <code>DISPATCHER</code>. <code>DISPATCHER</code>&#8216;s job is to call all of the <code>aspect.before</code> Functions, then the Function that is being advised (in this case <code>anotherMethod</code>) and then the <code>aspect.after</code> functions. (We&#8217;re ignoring <code>aspect.around</code> here, just for diagrammatic clarity&#8230; it doesn&#8217;t alter the explanation). You can see here, that <code>barAM</code> and <code>bar.anotherMethod</code> no longer point to the same thing.</p>

<p>Now, we add our second hitch:</p>

<p><img src="/images/dojo-problem/reference2.jpg" alt="Second hitch" /></p>

<p>Now, <code>bar.anotherMethod</code> is pointing to the AOP dispatcher, so that&#8217;s what <code>barAM2</code> points to. When we connect our second method:</p>

<p><img src="/images/dojo-problem/connect2.jpg" alt="Second connect" /></p>

<p><code>bar.anotherMethod</code> and <code>barAM2</code> are still pointing to the same thing. That explains what we saw in our console. Once AOP advice has been attached to a method, any newly created references to this method will see any advice attached <strong>after</strong> that reference is made</p>

<h1>Conclusions</h1>

<p>So what conclusions can we draw here?</p>

<p>The crucial one is that any references made to an object method <em>before</em> connections are made or AOP advice is attached will, when called, <strong>NOT</strong> result in these attachments being called.</p>

<p>Since the very nature of web-apps results is asynchronous, you often cannot guarantee the order of execution of code. In addition, future modifications to your code base, could result in these incompatible hitches and connections being made. This can lead to very tricky bugs.</p>

<p>So how do we avoid this? Luckily, it&#8217;s really easy. <code>hitch</code> allows you to provide a <code>method</code> as a String, rather than a Function. When you do this, your reference points to the method in a different way:</p>

<p><img src="/images/dojo-problem/safe_hitch.jpg" alt="Safe hitch" /></p>

<p>Your code would now look like:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>46
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> barAM <span style="color: #339933;">=</span> lang.<span style="color: #660066;">hitch</span><span style="color: #009900;">&#40;</span>bar<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;anotherMethod&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>Any time you make a call to your hitched Function, it has to look up <code>bar["aMethod"]</code>. Therefore, any changes to where this points are picked up by your hitched reference. This does have a minor penalty performance, but it makes your code <strong>way</strong> more robust, so it&#8217;s worth the cost.</p>

<p>So, in summary: whenever you create a hitch, either explicitly, or (as in the case of <code>connect</code>) implicitly, do so with the method as a String, rather than a Function. If you don&#8217;t, you may end up observing unexpected behaviour, once AOP comes in to play.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2012/05/06/problems-with-event-handling-and-aop-advice-in-dojo-when-hitch-is-in-play/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wrapped Tight. Made Free.</title>
		<link>http://www.danrumney.co.uk/2011/12/11/wrapped-tight-made-free/</link>
		<comments>http://www.danrumney.co.uk/2011/12/11/wrapped-tight-made-free/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 01:46:17 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Writing]]></category>
		<category><![CDATA[100 words]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=471</guid>
		<description><![CDATA[Some writing of mind, prompted by this post. The roar of the wind and surf makes it hard to concentrate. The acrid scent of molecules, once confined to the pages of textbooks, prick my nose and tickle my throat. Turning to face the wind, like a contrarian weather vane, sea-spray and sand scour my face. [...]]]></description>
			<content:encoded><![CDATA[<p>Some writing of mind, prompted by <a href="http://www.velvetverbosity.com/2011/12/06/100-words-cone-wonderment/">this post</a>.</p>

<blockquote>The roar of the wind and surf makes it hard to concentrate. The acrid scent of molecules, once confined to the pages of textbooks, prick my nose and tickle my throat. Turning to face the wind, like a contrarian weather vane, sea-spray and sand scour my face. The salted air mixes with salted water as the onslaught brings tears &#8211; tears born, not of emotion, but in an attempt to replenish what the cold rush has wicked away. The world blurs as I am disconnected from all useful stimuli. At that moment, its burdens drop away and for once, I am free.
</blockquote>

<hr/>

<script src="http://www.linkytools.com/basic_linky_include.aspx?id=119877" type="text/javascript" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2011/12/11/wrapped-tight-made-free/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Handling wrapping buffers with Perl</title>
		<link>http://www.danrumney.co.uk/2011/04/27/handling-wrapping-buffers-with-perl/</link>
		<comments>http://www.danrumney.co.uk/2011/04/27/handling-wrapping-buffers-with-perl/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 01:14:48 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[buffer]]></category>
		<category><![CDATA[sample code]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=389</guid>
		<description><![CDATA[I&#8217;m currently working on some Perl that processes memory dumps from a hardware appliance. Frequently, within these dumps, I&#8217;m face with a buffer that wraps. That is to say, each entry in the buffer is filled and, once the buffer is full, the next entry to be filled is the first, again (overwriting the old [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on some Perl that processes memory dumps from a hardware appliance.</p>

<p>Frequently, within these dumps, I&#8217;m face with a buffer that wraps. That is to say, each entry in the buffer is filled and, once the buffer is full, the next entry to be filled is the first, again (overwriting the old value).</p>

<p>At the point in time that the dump is taken, there is an index value that points to the &#8216;start&#8217; of this buffer (i.e. the point in the buffer that is the oldest). Unfortunately, when Perl gets hold of the buffer, it represents it as an array, with the first entry in the array being the buffer entry with the lowest memory address.</p>

<p>So&#8230; given an array representing a buffer and an index to the logical start of the buffer, what&#8217;s the simplest way to rejig it, so that the array represents the logical order of the buffer instead of the physical order?</p>

<p>Voila:</p>


<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">splice</span> <span style="color: #0000ff;">@buffer</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">splice</span> <span style="color: #0000ff;">@buffer</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$start_index</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2011/04/27/handling-wrapping-buffers-with-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handling arbitrary sort order in a database table</title>
		<link>http://www.danrumney.co.uk/2011/03/27/handling-arbitrary-sort-order-in-a-database-table/</link>
		<comments>http://www.danrumney.co.uk/2011/03/27/handling-arbitrary-sort-order-in-a-database-table/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 03:15:41 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=373</guid>
		<description><![CDATA[There are times when your web application needs to retain an arbitrary sort order for object. For example, you may have a slideshow of photographs and you want to be able to arrange them in any order you like. The simplest way to do this is to assign an attribute to each object that explicitly [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when your web application needs to retain an arbitrary sort order for object. For example, you may have a slideshow of photographs and you want to be able to arrange them in any order you like. The simplest way to do this is to assign an attribute to each object that explicitly represents its sort position.</p>

<p>This raises the question, when you decide to move an object to a new position in the sort order, what is the simplest way to update the other objects to ensure that you maintain consecutive sort positions.</p>

<p>Imagine the following SQL table:</p>


<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">&quot;projects&quot;</span> <span style="color: #FF00FF;">&#40;</span>
    <span style="color: #008000;">&quot;id&quot;</span> <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> AUTOINCREMENT <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
    <span style="color: #008000;">&quot;name&quot;</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">255</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
    <span style="color: #008000;">&quot;sort<span style="color: #008080; font-weight: bold;">_</span>order&quot;</span> <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>


<p>The &#8217;sort_order&#8217; field contains a simple integer that indicates the position that the object should appear in. To get the &#8216;projects&#8217; in the desired order, you&#8217;d execute:</p>


<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> projects <span style="color: #990099; font-weight: bold;">ORDER BY</span> sort_order <span style="color: #990099; font-weight: bold;">DESC</span></pre></div></div>


<p>To move an object from one position to another is a fairly simple operation. Each object has a unique (and immutable) &#8220;id&#8221;.  An appropriate method signature would be something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> change_sort_location<span style="color:#006600; font-weight:bold;">&#40;</span>object_id, new_location<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>


<p>The following steps should be followed:</p>

<ol>
    <li>Identify the current location of the objects as <code>old_location</code></li>
    <li>If <code>old_location &lt; new_location</code>
<ol>
    <li>Subtract 1 from <code>sort_order</code> for all objects where <code>old_location &lt; sort_order ≤ new_location</code></li>
</ol>
</li>
    <li>If <code>new_location &lt; old_location</code>
<ol>
    <li>Add 1 to <code>sort_order</code> for all objects where <code>new_location &lt; sort_order ≤ old_location</code></li>
</ol>
</li>
    <li>Set <code>sort_order</code> for the provided <code>id</code> to <code>new_location</code></li>
</ol>

<p>The SQL for this is pretty straightforward:</p>


<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">UPDATE</span> projects <span style="color: #990099; font-weight: bold;">SET</span> sort_order <span style="color: #CC0099;">=</span> sort_order<span style="color: #CC0099;">-</span><span style="color: #008080;">1</span>
  <span style="color: #990099; font-weight: bold;">WHERE</span> sort_order <span style="color: #CC0099;">&gt;</span> @old_location <span style="color: #CC0099; font-weight: bold;">AND</span> sort_order <span style="color: #CC0099;">&lt;=</span> @new_location</pre></div></div>


<p>or</p>


<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">UPDATE</span> projects <span style="color: #990099; font-weight: bold;">SET</span> sort_order <span style="color: #CC0099;">=</span> sort_order<span style="color: #CC0099;">+</span><span style="color: #008080;">1</span>
  <span style="color: #990099; font-weight: bold;">WHERE</span> sort_order <span style="color: #CC0099;">&gt;</span> @new_location <span style="color: #CC0099; font-weight: bold;">AND</span> sort_order <span style="color: #CC0099;">&lt;=</span> @old_location</pre></div></div>


<p>followed by</p>


<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">UPDATE</span> projects <span style="color: #990099; font-weight: bold;">SET</span> sort_order <span style="color: #CC0099;">=</span> @new_location
  <span style="color: #990099; font-weight: bold;">WHERE</span> id <span style="color: #CC0099;">=</span> @object_id</pre></div></div>


<p>Clearly, in a full implementation, you&#8217;d probably not be using SQL variables, but the point stands.</p>

<p>One complaint might be that the object that is getting moved can get updated twice here, but that&#8217;s unlikely to be a major performance impact.</p>

<p>Currently, once object move requires 2 SQL queries. <i>N</i> object moves will require <i>2N</i> SQL queries. I&#8217;m currently trying to figure out a method to reduce the number of SQL queries needed for multiple moves.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2011/03/27/handling-arbitrary-sort-order-in-a-database-table/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to point your Web address to your Web site</title>
		<link>http://www.danrumney.co.uk/2010/04/06/how-to-point-your-web-address-to-your-web-site/</link>
		<comments>http://www.danrumney.co.uk/2010/04/06/how-to-point-your-web-address-to-your-web-site/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 23:29:32 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=319</guid>
		<description><![CDATA[Many readers will already know this, but I&#8217;ve had call to walk people through this process a few times, recently, so I&#8217;m putting directions online for future reference. Let&#8217;s say you have a website hosted somewhere, and you&#8217;ve bought the domain name from Go Daddy. How do you make that web address point to your [...]]]></description>
			<content:encoded><![CDATA[<p>Many readers will already know this, but I&#8217;ve had call to walk people through this process a few times, recently, so I&#8217;m putting directions online for future reference.</p>

<p>Let&#8217;s say you have a website hosted somewhere, and you&#8217;ve bought the domain name from <a href="http://www.godaddy.com/">Go Daddy</a>. How do you make that web address point to your website? Well, it&#8217;s pretty straightforward.</p>

<p>First, log on to your account. Once you have, you should see a page similar to this:</p>

<p style="text-align: center;"><a href="http://www.danrumney.co.uk/blog/wp-content/uploads/2010/04/account.jpg"><img class="size-full wp-image-320 aligncenter" style="clear: right;" title="Go Daddy Account page" src="http://www.danrumney.co.uk/blog/wp-content/uploads/2010/04/account.jpg" alt="" width="450" height="296" /></a></p>

<p>Select &#8216;Domain Manager&#8217; and the next window will open up:</p>

<p><a href="http://www.danrumney.co.uk/blog/wp-content/uploads/2010/04/domain_manager.jpg"><img class="aligncenter size-full wp-image-322" title="Go Daddy Domain Manager" src="http://www.danrumney.co.uk/blog/wp-content/uploads/2010/04/domain_manager.jpg" alt="" width="450" height="220" /></a></p>

<p>On this page, select the domain name that you&#8217;re interested in and then click on the Nameservers. The following window will pop up:</p>

<p><a href="http://www.danrumney.co.uk/blog/wp-content/uploads/2010/04/dns_settings.jpg"><img class="aligncenter size-full wp-image-323" title="Go Daddy Nameserver Settings" src="http://www.danrumney.co.uk/blog/wp-content/uploads/2010/04/dns_settings.jpg" alt="" width="450" height="248" /></a></p>

<p>Choose &#8216;I have specific nameservers for my domain&#8217; and insert the nameservers that your hosting solution provided.</p>

<p>Once you click OK, it may take some minutes for the change to propagate throughout the internet. Technically speaking, it can take many hours to get to <strong>every</strong> part of the Internet, but most people will see the change within a few minutes.</p>

<p>That&#8217;s it&#8230; easy, eh?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2010/04/06/how-to-point-your-web-address-to-your-web-site/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making your tweets ReTweetable</title>
		<link>http://www.danrumney.co.uk/2010/03/02/making-your-tweets-retweetable/</link>
		<comments>http://www.danrumney.co.uk/2010/03/02/making-your-tweets-retweetable/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 22:57:32 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[greasemonkey]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=310</guid>
		<description><![CDATA[We all know, by now, that Twitter limits its tweets to 140 characters. We&#8217;ve all got pretty good at limiting ourselves to 140 characters, but many overlook a hidden limit. This post outlines what that is and how we can avoid it. Many users of Twitters are hoping that their followers will retweet (RT) their [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" title="Twitter and Greasemonkey" src="http://danrumney.co.uk/images/tweetMonkey.jpg" alt="" width="510" height="156" /></p>

<p>We all know, by now, that Twitter limits its tweets to 140 characters. We&#8217;ve all got pretty good at limiting ourselves to 140 characters, but many overlook a hidden limit. This post outlines what that is and how we can avoid it.</p>

<p>Many users of Twitters are hoping that their followers will retweet (RT) their tweets. Twitter recently made a change to how these work, but in general, the following pattern is followed:</p>

<pre>   UserXYZ tweets: Hey... here's something that's fascinating
   UserABC tweets: RT @UserXYZ: Hey... here's something that's fascinating</pre>

<p>User XYZ&#8217;s tweet was 42 characters. UserABC&#8217;s RT was 52 characters, i.e. 10 characters were added in order to RT.</p>

<p>Put another way, if UserXYZ creates a tweet that was longer than 130 characters, nobody would be able to RT it with modifying the original tweet. If you&#8217;re trying to get a specific message out to the world, you might not be happy with lots of people fiddling with it.</p>

<p>I&#8217;ve created a new Greasemonkey script which will help you with this. I&#8217;ve written about Greasemonkey plugins <a href="http://www.danrumney.co.uk/2009/02/15/augmenting-twitter-whoami/">before</a> and this is another Twitter helper. If you install the script, you will see the following change:</p>

<p style="text-align: center;"><a href="http://www.danrumney.co.uk/blog/wp-content/uploads/2010/03/retweetable_screenshot.png"><img class="aligncenter size-full wp-image-311" title="retweetable_screenshot" src="http://www.danrumney.co.uk/blog/wp-content/uploads/2010/03/retweetable_screenshot.png" alt="" width="600" height="160" /></a></p>

<p style="text-align: left;">You can now see, next to the normal character countdown, a bracketed countdown. This is the number of characters that you have left, before a tweet can no longer be RTed without modification. In this example, you would be able to send the tweet (as you have 6 characters left), but Twitter users would have to remove 8 characters before they could RT your Tweet.</p>

<p style="text-align: left;">To use this, it&#8217;s simple:</p>

<ol>
    <li>If you haven&#8217;t already, install <a href="https://addons.mozilla.org/en-US/firefox/addon/748">Greasemonkey</a></li>
    <li>Install the <a href="http://danrumney.co.uk/gmScripts/reTweetable.user.js">ReTweetable Alert</a> script</li>
</ol>

<p>That&#8217;s it! As ever, your questions and comments are most welcome</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2010/03/02/making-your-tweets-retweetable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
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="http://www.danrumney.co.uk/js/seoSafeRotatingBanner.js">here</a>.
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.
<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="http://www.danrumney.co.uk/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
<em>interval</em> is the time (in ms) between each rotation
<em>defaultTarget</em> is the desired value for the TARGET attribute of the banner link
<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
<em>href</em> is the HREF of the banner link
<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>RubyGems fun with a VPS server</title>
		<link>http://www.danrumney.co.uk/2009/10/07/rubygems-fun-with-a-vps-server/</link>
		<comments>http://www.danrumney.co.uk/2009/10/07/rubygems-fun-with-a-vps-server/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 00:01:20 +0000</pubDate>
		<dc:creator>dancrumb</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubygems]]></category>

		<guid isPermaLink="false">http://www.danrumney.co.uk/?p=264</guid>
		<description><![CDATA[All of my websites are currently running on a VPS server provided by HostIcan. I recently discovered a little quirk involving VPS servers and RubyGems. I&#8217;ve started learning Ruby on Rails and the Ruby part uses things called &#8216;Gems&#8217; in a similar way to Perl using Modules. Where you read &#8216;gem&#8217;, thing &#8216;cpan&#8217;. I wanted [...]]]></description>
			<content:encoded><![CDATA[<p>All of my websites are currently running on a VPS server provided by HostIcan. I recently discovered a little quirk involving VPS servers and RubyGems.</p>

<p>I&#8217;ve started learning Ruby on Rails and the Ruby part uses things called &#8216;Gems&#8217; in a similar way to Perl using Modules. Where you read &#8216;gem&#8217;, thing &#8216;cpan&#8217;.</p>

<p>I wanted to install a new gem on my server to support some Paypal integration, but &#8216;gem&#8217; kept segfaulting on me.</p>

<p>If you&#8217;re like me, you&#8217;ll see this behaviour:</p>

<pre>root@server [~]# gem install rubygems-update
Bulk updating Gem source index for: http://gems.rubyforge.org
Terminated</pre>

<p>It turns out that &#8216;Bulk updating&#8230;&#8217; part gobbles up memory like it&#8217;s going spare and leads to a segfault.</p>

<p>The way to avoid this problem is to update RubyGems&#8230; but to do that, you need to use &#8216;gem&#8217;&#8230; and that leads to a segfault&#8230; and around we go again.</p>

<p>To break the cycle, you simply use:</p>

<pre>gem update --system --no-update-sources</pre>

<p>to prevent the updating of sources.</p>

<p>Once I&#8217;d done that, I found that I was <strong>still</strong> getting segmentation faults. Also, when I ran:</p>

<pre>root@server [~]# gem install activemerchant --no-update-sources</pre>

<p>I now got</p>

<pre>ERROR:  could not find activemerchant locally or in a repository</pre>

<p>So the problem still wasn&#8217;t resolved!</p>

<p>The only solution was to force my system to the latest version of RubyGems. Unfortunately, this was not in my local repository. However, a manual update was pretty simple:</p>

<pre>cd /tmp
wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
tar -xvzf rubygems-1.3.5.tgz
cd rubygems-1.3
ruby setup.rb</pre>

<p>Job done!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danrumney.co.uk/2009/10/07/rubygems-fun-with-a-vps-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

