<?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>Stephen McIntyre &#187; XHTML</title>
	<atom:link href="http://stephenmcintyre.net/category/blog/xhtml/feed/" rel="self" type="application/rss+xml" />
	<link>http://stephenmcintyre.net</link>
	<description>Young Scottish Web Designer</description>
	<lastBuildDate>Fri, 25 Nov 2011 00:10:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Untold Entertainment Made in jQuery Part 3 &#8211; The Project Slider</title>
		<link>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-project-slider/</link>
		<comments>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-project-slider/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 15:00:46 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=577</guid>
		<description><![CDATA[
Being a showcase for the Flash work of Untold Entertainment, the image slider is used as the main focus of the home page and displays his latest projects. Continuing from the first two tutorials, I&#8217;ll now show you how to create this slider with jQuery.
We&#8217;ll be treating the slider as a separate element from our [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/untold-slider.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/untold-slider.png" alt="" title="untold-slider" width="400" height="308" class="aligncenter size-full wp-image-584" /></a></p>
<p>Being a showcase for the Flash work of <a href="http://untoldentertainment.com">Untold Entertainment</a>, the image slider is used as the main focus of the home page and displays his latest projects. Continuing from the <a href="/blog/untold-entertainment-in-jquery-animating-twitter-text/">first</a> <a href="/blog/untold-entertainment-in-jquery-styling-the-twitter-bar/">two</a> tutorials, I&#8217;ll now show you how to create this slider with jQuery.</p>
<p>We&#8217;ll be treating the slider as a separate element from our Twitter bar before, so no need to use old code here.</p>
<p>With the abundance of jQuery image gallery plugins and scripts it&#8217;s safe to say that jQuery makes this sort of thing in particular run a lot more smoothly than with plain JavaScript. This section will be the easiest part of the series.</p>
<p>Let&#8217;s dive in.<span id="more-577"></span></p>
<p>First have a look at the <a href="/examples/untold/part-3.html">slider demo</a>.</p>
<h3>The Markup</h3>
<p>We&#8217;ll start with the HTML tags.</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
</pre></td><td 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;slider&quot;</span>&gt;</span>
  <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;image&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;prev&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;&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;next&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;&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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <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;content&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;&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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h3</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p><strong>#slider:</strong> The container element that wraps everything.</p>
<p><strong>#image:</strong> This contains the large image and navigation buttons (<code>#prev</code> and <code>#next</code>). The image is actually a background, but more on that later.</p>
<p><strong>#content:</strong> Holds elements for the project title (<code>&lt;h3&gt;</code>) and excerpt (<code>&lt;p&gt;</code>). The <code>&lt;a&gt;</code> inside the title creates a link to the page that the project is on.</p>
<h3>Setting Up jQuery and the Projects Array</h3>
<p>If you&#8217;ve followed the previous tutorials, including jQuery is still as straight-forward as it was before. Again, we&#8217;ll be drawing it in from the Google Code repository.</p>
<p>I&#8217;ve also added an extra empty tag that we can add our own code into.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>JavaScript should go above the main HTML content (at the start of the <code>&lt;body&gt;</code> if your document has it).</p>
<p>Inside the empty script tag, set up our &#8220;projects&#8221; array like so.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">var</span> projects <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/bestEd/hedgeYourBets/thumb.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Best Ed – Hedge Your Bets!&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2009/09/17/best-ed-hedge-your-bets/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/bestEd/crossingDog/thumb.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Best Ed – Crossing Dog&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2009/09/17/best-ed-crossing-dog/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/bloat/bloat.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Bloat.&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Our company president created Bloat. in roughly twenty-two hours at TOJam 4, a weekend-long game development expo. Bloat. is a true [...]&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2009/05/05/bloat/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/AWTY/eyeInTheSky/EyeInTheSky.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Eye in the Sky&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;We created this spot-the-differences game for Sinking Ship Entertainment’s Are We There Yet? World Adventure kids’ travel teevee [...]&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2008/11/28/eye-in-the-sky/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span>
    image<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/img/projects/AWTY/trainTrack/trainTrack.jpg&quot;</span><span style="color: #339933;">,</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Train Track!&quot;</span><span style="color: #339933;">,</span>
    excerpt<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;We were approached by Sinking Ship Entertainment to create an eye-spy game for their kids’ travel teevee show Are We There Yet? World [...]&quot;</span><span style="color: #339933;">,</span>
    link<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://www.untoldentertainment.com/blog/2008/11/28/train-track/&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> project_id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The script uses JSON notation to create an array of objects, each with the same set of attributes: &#8220;image&#8221;, &#8220;title&#8221;, &#8220;excerpt&#8221; and &#8220;link&#8221;.</p>
<p>We would print these onto the page using PHP, looping through database rows in a WordPress setup. This won&#8217;t be covered as it&#8217;s irrelevant how the information gets to the page. This set of example project data will work on its own for now.</p>
<p>The <code>project_id</code> variable holds the array key for our current project data object. This means we can change this number (with the navigation buttons) and call a whole new set of data when we want it to load.</p>
<h3>Attaching Clicks to the Navigation Buttons</h3>
<p>To allow ourselves to reference our HTML elements, we have to wait for them to load before we call our JavaScript to interact with them. For this we can either place the JavaScript after our HTML, or use jQuery&#8217;s nice &#8220;document ready&#8221; check to do this for us, and still keep our JavaScript at the top.</p>
<p>Here I&#8217;ll use the jQuery method. Generally it lets us to load all of our JavaScript first, allowing a smoother page load and cuts out some jerky effects.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  $<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#prev'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      project_id<span style="color: #339933;">--;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>project_id <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        project_id <span style="color: #339933;">=</span> projects.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      load_project<span style="color: #009900;">&#40;</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;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#next'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      project_id<span style="color: #339933;">++;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>project_id <span style="color: #339933;">&gt;</span> projects.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        project_id <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      load_project<span style="color: #009900;">&#40;</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;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    load_project<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In each case, clicking one of the buttons adds or removes one from <code>project_id</code>. If it is less than zero or greater than our maximum index in <code>projects</code> (five) then it is reset to the other side of the scale.</p>
<p>For example, pressing the previous button when you are looking at the first project (ID of zero), the slider skips to the fifth project in the list. Clicking next from here takes you back to the first element, and so on.</p>
<h3>load_project()</h3>
<p>This is what is called when the page is loaded and when a navigation button is clicked. It takes the <code>project_id</code> and populates the slider with all information attached to that array key inside <code>projects</code>.</p>
<p>I&#8217;ll go through this one a bit at a time.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> load_project<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> project_data <span style="color: #339933;">=</span> projects<span style="color: #009900;">&#91;</span>project_id<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#image'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'backgroundImage'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content h3 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> project_data.<span style="color: #660066;">link</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>project_data.<span style="color: #660066;">title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>project_data.<span style="color: #660066;">excerpt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>project_data.<span style="color: #660066;">excerpt</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#content p'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' &lt;a href=&quot;'</span> <span style="color: #339933;">+</span> project_data.<span style="color: #660066;">link</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;keep reading&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;img src=&quot;'</span> <span style="color: #339933;">+</span> project_data.<span style="color: #660066;">image</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; /&gt;'</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>responseText<span style="color: #339933;">,</span> textStatus<span style="color: #339933;">,</span> XMLHttpRequest<span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#image'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'backgroundImage'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;'</span> <span style="color: #339933;">+</span> project_data.<span style="color: #660066;">image</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Line 5:</strong> Removes any overridden background image styles on &#8220;#image&#8221; to display the &#8220;loading&#8221; spinner.</p>
<p><strong>Lines 7-9:</strong> Adds an <code>href</code> to the title allowing it to link to an external page, and fills in the title text. These are both taken from the current item in the list of projects (referenced here as <code>project_data</code>).</p>
<p><strong>Lines 11-16:</strong> The first line fills in the <code>&lt;p&gt;</code> tag with the excerpt text. If the excerpt is not an empty string, an extra &#8220;keep reading&#8221; link is also added to the end of the text linking to the project page. This is the same link used for the title.</p>
<p><strong>Lines 18 and 19:</strong> jQuery creates a DOM element of an image, with its source linking to the current project&#8217;s image URL. We can then use this to track how long it takes to load, as done here with the built-in <code>.load()</code> method.</p>
<p>When the element has loaded (read: the image has been fully downloaded into your browser), it issues a callback to JavaScript. Anything we put inside of this callback will only be run once the requested image is available to be displayed. The main reason here being to switch out the &#8220;loading&#8221; spinner.</p>
<p><strong>Line 21:</strong> Here we edit the background of the &#8220;#image&#8221; container again, this time changing it to the image for the current project and overriding the spinner.</p>
<h3>Adding Style</h3>
<p>If you&#8217;re familiar with basic CSS, these will be pretty straight-forward.</p>
<p>However, pay particular attention to the <code>:hover</code> pseudo on the navigation buttons. Since we want the buttons to animate when we hover over them and not on its own or on a timer, it&#8217;s useful to do this with CSS here instead of JavaScript.</p>
<p>Again, this part goes right at the top before everything else (in the <code>&lt;head&gt;</code> section if your document has one).</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
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
  <span style="color: #00AA00;">*</span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#slider</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">466px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #3333ff;">:Verdana</span><span style="color: #00AA00;">,</span> Geneva<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#image</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'loader.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">340px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#content</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#image</span> a
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">280px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span><span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background</span><span style="color: #3333ff;">:<span style="color: #993333;">none</span> </span>url<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'prev.png'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#222</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  a<span style="color: #cc00cc;">#prev</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'prev.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  a<span style="color: #cc00cc;">#prev</span><span style="color: #3333ff;"><span style="color: #00AA00;">:</span>hover
  </span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'prev-hover.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  a<span style="color: #cc00cc;">#next</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'next.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  a<span style="color: #cc00cc;">#next</span><span style="color: #3333ff;"><span style="color: #00AA00;">:</span>hover
  </span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'next-hover.gif'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></td></tr></table></div>

<p>And that&#8217;s it. Here is what your final slider will look like.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/untold-js-slider.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/untold-js-slider.png" alt="" title="untold-js-slider" width="400" height="381" class="aligncenter size-full wp-image-590" /></a></p>
<p>Now comes the fun part. Next week I&#8217;ll be showing you how to make that roll-out tongue on the menu bar, all without a bit of Flash.</p>
<p>Connect with my blog&#8217;s <a href="http://feeds.feedburner.com/stephenmcintyre?format=html">news stream</a> to get notified as soon as it&#8217;s up.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-project-slider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Untold Entertainment Made in jQuery Part 2 – Styling the Twitter Bar</title>
		<link>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-styling-the-twitter-bar/</link>
		<comments>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-styling-the-twitter-bar/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 00:50:17 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=556</guid>
		<description><![CDATA[Carrying on from the first part of creating a Flash-less home page on Untold Entertainment, we add to the tutorial on animating Twitter text with some more usability and style.
We start off by generating links (to URLs and Twitter pages) inside the text, make a couple of buttons to skip the tweets backwards and forwards, [...]]]></description>
			<content:encoded><![CDATA[<p>Carrying on from the first part of creating a Flash-less home page on <a href="http://untoldentertainment.com">Untold Entertainment</a>, we add to the tutorial on <a href="/blog/untold-entertainment-in-jquery-animating-twitter-text/">animating Twitter text</a> with some more usability and style.</p>
<p>We start off by generating links (to URLs and Twitter pages) inside the text, make a couple of buttons to skip the tweets backwards and forwards, then animate the Twitter bird and speech bubble as a background.<span id="more-556"></span></p>
<p>Try out the new <a href="/examples/untold/part-2.html">styled Twitter bar demo</a>.</p>
<h3>The New HTML Structure</h3>
<p>To add in our new elements, we&#8217;ll have to mess about with the layout of our HTML tags.</p>
<p>The container still holds the text as before, but we add in the bird image and navigation buttons inside a div. The holder is there to keep the buttons beside the text, and the outer wrapper collects all of it including the bird image.</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="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;wrapper&quot;</span>&gt;</span>
  <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;holder&quot;</span>&gt;</span>
    <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;tweet_buttons&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;prev&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;#9668;</span><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;next&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;#9658;</span><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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <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;container&quot;</span>&gt;</span>
&nbsp;
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bird&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bird.gif&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;bird&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>The HTML entities are the forward and backward pointing arrowheads you see. These can be images, but the entities will do for now to make things simpler.</p>
<h3>Adding Links to the Text</h3>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-static.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-static.png" alt="" title="bird-static" width="400" height="39" class="aligncenter size-full wp-image-570" /></a></p>
<p>To find URLs and Twitter usernames, we have to do a check on each word. Here we&#8217;re looking for anything starting with &#8220;http://&#8221; and &#8220;@&#8221; respectively.</p>
<p>We change our <code>add_text()</code> method from last time with the following lines of code. This should replace the line beginning with <code>var new_text = ...</code>.</p>
<p>The first three sections change text inside <code>add_text()</code>. For the full replacement code, use the new function at the end of the tutorial.</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;"><span style="color: #003366; font-weight: bold;">var</span> word <span style="color: #339933;">=</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>word_key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><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: #3366CC;">'@'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  word <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;a href=&quot;http://twitter.com/'</span> <span style="color: #339933;">+</span> word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'http://'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  word <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;a href=&quot;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> new_text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &lt;span class=&quot;rot'</span> <span style="color: #339933;">+</span> rand <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This wraps any usernames and URLs inside an anchor <code>&lt;a&gt;</code> tag and turns them into clickable links.</p>
<h3>Animating the bird</h3>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-chirp.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-chirp.png" alt="" title="bird-chirp" width="400" height="38" class="aligncenter size-full wp-image-569" /></a></p>
<p>These few lines go after <code>$('#container').html(new_text);</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</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> <span style="color: #3366CC;">'bird_shout.gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>chirp_time<span style="color: #339933;">,</span> <span style="color: #3366CC;">'blabel_'</span> <span style="color: #339933;">+</span> update_key<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</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> <span style="color: #3366CC;">'bird.gif'</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>The variable <code>chirp_time</code> should be added to our list of Variables at the top of the JavaScript section. Here I&#8217;ve set it to 100 as it works well for the GIF animation. And while you&#8217;re there add in a delay that we&#8217;ll use later.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">...
<span style="color: #003366; font-weight: bold;">var</span> length_min <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> length_max <span style="color: #339933;">=</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> chirp_time <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> delay <span style="color: #339933;">=</span> <span style="color: #CC0000;">2000</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is what makes the bird&#8217;s mouth move, held open by the time we&#8217;ve specified in <code>chirp_time</code> as 100 milliseconds.</p>
<h3>Animating the Speech Bubble</h3>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-transition.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/06/bird-transition.png" alt="" title="bird-transition" width="400" height="37" class="aligncenter size-full wp-image-571" /></a></p>
<p>Look for the &#8220;if&#8221; statement of <code>if(word_key < updates...</code>, add this line just inside.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This sets the background to be the non-animated image of a speech bubble.</p>
<p>In the "else" of this statement, look for the first timer event (the first line inside). In here we add the code to make the background animate as the text transitions into a new tweet. It's similar to the line above, but instead links to an animated image.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble_animated.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Setting Up the Controls</h3>
<p>To add JavaScript "onclick" events to our navigational buttons, we call jQuery on page load. This is to allow the elements to be created before we bind events to them.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#prev'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">stopTime</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;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    update_key<span style="color: #339933;">--;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>update_key <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      update_key <span style="color: #339933;">=</span> updates.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble_animated.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>delay<span style="color: #339933;">,</span> <span style="color: #3366CC;">'label_'</span> <span style="color: #339933;">+</span> word_key<span style="color: #339933;">,</span> add_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#next'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">stopTime</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;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    update_key<span style="color: #339933;">++;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>update_key <span style="color: #339933;">&gt;</span> updates.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      update_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble_animated.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>delay<span style="color: #339933;">,</span> <span style="color: #3366CC;">'label_'</span> <span style="color: #339933;">+</span> word_key<span style="color: #339933;">,</span> add_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code should look very familiar to what's inside <code>add_text()</code>. Simply put, the <code>#previous</code> code skips to the previous tweet and <code>#next</code> goes to the one after, so this changes the increment or decrement of the <code>update_key</code> variable.</p>
<h3>Updating the Stylesheet</h3>
<p>The new styles for these elements are also as follows.</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
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#bubble</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">700px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">60px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #3333ff;">:inline-</span>block<span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'bubble.gif'</span><span style="color: #00AA00;">&#41;</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span> <span style="color: #933;"><span style="color: #cc66cc;">50</span>%</span> <span style="color: #993333;">no-repeat</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#buttons</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">right</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span><span style="color: #933;">40px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
  <span style="color: #cc00cc;">#buttons</span> a
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#999</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#bird</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">vertical-align</span><span style="color: #00AA00;">:</span><span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>And that's it for now.</p>
<p>Next we'll make the image slider.</p>
<p>(Here's the new code for <code>add_text()</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
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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> add_text<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> rand <span style="color: #339933;">=</span> Math.<span style="color: #660066;">ceil</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> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> word <span style="color: #339933;">=</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>word_key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><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: #3366CC;">'@'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    word <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;a href=&quot;http://twitter.com/'</span> <span style="color: #339933;">+</span> word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>word.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'http://'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    word <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;a href=&quot;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/a&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> new_text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &lt;span class=&quot;rot'</span> <span style="color: #339933;">+</span> rand <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> word <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>new_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</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> <span style="color: #3366CC;">'bird_shout.gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>chirp_time<span style="color: #339933;">,</span> <span style="color: #3366CC;">'blabel_'</span> <span style="color: #339933;">+</span> update_key<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bird'</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> <span style="color: #3366CC;">'bird.gif'</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;
  word_key<span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>word_key <span style="color: #339933;">&lt;</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble.jpg&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    set_timer<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: #000066; font-weight: bold;">else</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>wait<span style="color: #339933;">,</span> <span style="color: #3366CC;">'reset'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
      update_key<span style="color: #339933;">++;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>update_key <span style="color: #339933;">&gt;</span> updates.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        update_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#bubble'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-image'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'url(&quot;bubble_animated.gif&quot;)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>delay<span style="color: #339933;">,</span> <span style="color: #3366CC;">'label_'</span> <span style="color: #339933;">+</span> word_key<span style="color: #339933;">,</span> add_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-styling-the-twitter-bar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Untold Entertainment Made in jQuery Part 1 &#8211; Animating Twitter Text</title>
		<link>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-animating-twitter-text/</link>
		<comments>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-animating-twitter-text/#comments</comments>
		<pubDate>Thu, 27 May 2010 23:14:55 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=531</guid>
		<description><![CDATA[
Ryan Creighton&#8217;s Flash development site Untold Entertainment has a quirky home page with plenty of animation and things to click on (complete with roll-out tongue) and of course is built in Flash. Thing is, I&#8217;m against any kind of Flash interface. It&#8217;s great for games, but otherwise it&#8217;s unnecessary (which includes videos).
With the whole Adobe [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold.png" alt="" title="untold" width="400" height="244" class="aligncenter size-full wp-image-552" /></a></p>
<p>Ryan Creighton&#8217;s Flash development site <a href="http://untoldentertainment.com">Untold Entertainment</a> has a quirky home page with plenty of animation and things to click on (complete with roll-out tongue) and of course is built in Flash. Thing is, I&#8217;m against any kind of Flash interface. It&#8217;s great for games, but otherwise it&#8217;s unnecessary (which includes videos).</p>
<p>With the whole Adobe v Apple debate that&#8217;s going on right now (yes, I said I wouldn&#8217;t mention it but it applies here &#8216;kay), it&#8217;s safe to say you&#8217;d be better placing your chips on the JavaScript and HTML5 end of the table.</p>
<p>The discouraging part is that his website could be done without Flash as most of the scrolling and animating could be done with straight JavaScript, and jQuery will help see things along smoothly.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold-twbar.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold-twbar.png" alt="" title="untold-twbar" width="400" height="38" class="aligncenter size-full wp-image-553" /></a></p>
<p>Today I&#8217;ll show you how to animate the text at the top, feeding in from <a href="http://twitter.com/untoldent">his Twitter feed</a>, complete with jerky timings, slanted text and collecting feed data.<span id="more-531"></span></p>
<p>Check out the <a href="/examples/untold/part-1.html">Untold Twitter bar</a> demo.</p>
<h3>Including jQuery, Timer plugin, and Google AJAX Feed API</h3>
<p>We&#8217;ll start off by including our libraries and plugins. Here I use jQuery, the jQuery Timers plugin, and the Google AJAX Feed API &#8211; all sourced from their Google Code hosts.</p>
<p>Admittedly we won&#8217;t be using much jQuery here as the code changed quite a bit as it got added to, but it might come in handy in later tutorials. If you know how to change the scripts into plain JavaScript, by all means change those parts over.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stikked.googlecode.com/svn/trunk/static/js/jquery.timers.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.com/jsapi?key=YOURAPIKEY&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>More information on the API key can be found in my earlier post on <a href="/blog/guide-to-google-ajax-feed-api/">Google AJAX Feeds</a>.</p>
<p>This code goes inside the <code>&lt;body&gt;</code> element, right at the top so it loads first. There&#8217;s an extra element in there &#8211; this is where we&#8217;ll add our own JavaScript.</p>
<h3>Settings and Variables</h3>
<p>To allow us to fine tune the feel of the page later, we&#8217;ll set up a list of variables. This bit of code is the first to go inside the empty <code>&lt;script&gt;</code> tag above.</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;"><span style="color: #006600; font-style: italic;">/* Variables - tweak as you like */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> username <span style="color: #339933;">=</span> <span style="color: #3366CC;">'UntoldEnt'</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> feed_url <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://twitter.com/statuses/user_timeline/21834006.rss'</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> entry_fetches <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> interval <span style="color: #339933;">=</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> wait <span style="color: #339933;">=</span> <span style="color: #CC0000;">6000</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> length_min <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> length_max <span style="color: #339933;">=</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>These should be reasonable to understand, and what isn&#8217;t obvious will be covered later as the script develops.</p>
<h3>Collecting Feed Data</h3>
<p>In this example we want to collect the data from Twitter, or more specifically <a href="http://twitter.com/untoldent">@untoldent</a>&#8217;s account. More information on the use of the <code>google</code> object can be found on the same link as above (the <a href="/blog/guide-to-google-ajax-feed-api/">Google Feed</a> one).</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Google Feed API stuff */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> updates <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: #003366; font-weight: bold;">function</span> Initialize<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> feed <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">feeds</span>.<span style="color: #660066;">Feed</span><span style="color: #009900;">&#40;</span>feed_url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feed.<span style="color: #660066;">setNumEntries</span><span style="color: #009900;">&#40;</span>entry_fetches<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  feed.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<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><span style="color: #339933;">!</span>result.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> result.<span style="color: #660066;">feed</span>.<span style="color: #660066;">entries</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> entry <span style="color: #339933;">=</span> result.<span style="color: #660066;">feed</span>.<span style="color: #660066;">entries</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">title</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span>username.<span style="color: #660066;">length</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>entry.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><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: #3366CC;">'@'</span> <span style="color: #339933;">&amp;&amp;</span> entry.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'RT'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
          updates.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>entry.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    set_timer<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: #009900;">&#125;</span>
&nbsp;
google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;feeds&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
google.<span style="color: #660066;">setOnLoadCallback</span><span style="color: #009900;">&#40;</span>Initialize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We start off by collecting the RSS feed from the <code>feed_url</code> and check there are no errors like I did before.</p>
<p>The script then loops through each feed element once it has been loaded in and removes replies and retweets. What&#8217;s left is added to our array of <code>updates</code>, split word by word into another array inside of it.</p>
<p>When this is done, <code>set_timer()</code> is called to start the actual stuff that makes it look like Untold&#8217;s Twitter bar.</p>
<h3>Creating Animated Text</h3>
<p>Last bit of JavaScript &#8211; the fancy part.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/* Animated Tweet stuff */</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> clamp<span style="color: #009900;">&#40;</span>val<span style="color: #339933;">,</span> min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> Math.<span style="color: #660066;">min</span><span style="color: #009900;">&#40;</span>max<span style="color: #339933;">,</span> val<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> update_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> set_timer<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> length <span style="color: #339933;">=</span> clamp<span style="color: #009900;">&#40;</span>updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>word_key<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> length_min<span style="color: #339933;">,</span> length_max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> timing <span style="color: #339933;">=</span> length <span style="color: #339933;">*</span> interval<span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>timing<span style="color: #339933;">,</span> <span style="color: #3366CC;">'label_'</span> <span style="color: #339933;">+</span> word_key<span style="color: #339933;">,</span> add_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> add_text<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> rand <span style="color: #339933;">=</span> Math.<span style="color: #660066;">ceil</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> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> new_text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' &lt;span class=&quot;rot'</span> <span style="color: #339933;">+</span> rand <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;'</span> <span style="color: #339933;">+</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>word_key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>new_text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  word_key<span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>word_key <span style="color: #339933;">&lt;</span> updates<span style="color: #009900;">&#91;</span>update_key<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    set_timer<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: #000066; font-weight: bold;">else</span>
  <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">oneTime</span><span style="color: #009900;">&#40;</span>wait<span style="color: #339933;">,</span> <span style="color: #3366CC;">'reset'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      word_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
      update_key<span style="color: #339933;">++;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>update_key <span style="color: #339933;">&gt;</span> updates.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        update_key <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      set_timer<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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><b>clamp:</b> In other languages, <code>clamp</code> is usually available inside a <code>Math</code> library, but not this one. So I&#8217;ve added this to make the code cleaner later.</p>
<p><b>word_key and update_key:</b> These are indexes for the <code>updates</code> array and the words inside each element. These increment by one as they are used, and then reset to zero when moving to a new element. They&#8217;re kept outside the functions so that they can be called from wherever.</p>
<p><b>set_timer:</b> First calculates a timing based on the length of the word. To avoid certain words appearing too quickly or slowly, the <code>length_min</code> and <code>length_max</code> are used from earlier to add a constrained limit on either side. It is then multiplied by <code>interval</code> &#8211; also set earlier &#8211; to work out our timer.</p>
<p>The Timers plugin is then used to add a timer to the container element. This will call <code>add_text()</code> after the set time from our previous word length is up.</p>
<p><b>add_text:</b> Starts by setting a random number. This sets our rotation later, based on four classes in a stylesheet. Each word is contained within a <code>&lt;span&gt;</code> element to treat each rotation separately.</p>
<p>The <code>.html()</code> method inside jQuery allows us to add HTML markup inside of our container. Here we add our new element (the <code>&lt;span&gt;</code> with a new word) onto the existing DOM.</p>
<p>When the word has been added, the index increases by one to access the next word. A timer is then set to add this word to the queue, which then adds it on the same way as before.</p>
<p>If there are no words left (i.e., the tweet has ended), the script stops for a period defined by <code>wait</code> and some variables are reset to prepare for a new word. This includes <code>word_key</code>, <code>update_key</code> (if needed), and resets the HTML inside the container back to being empty.</p>
<p>Again, the timer is reset to prepare for new words to be added in, once the script has come out of its pause.</p>
<h3>Rotation Styles</h3>
<p>To keep with the quirky feel of Untold&#8217;s header, the text in the tweet bar also tilts word-by-word. As we have already set up our span elements with rotation classes, all that&#8217;s left is to style them.</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="css" style="font-family:monospace;">&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
<span style="color: #cc00cc;">#container</span>
<span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">1.4em</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #3333ff;">:Verdana</span><span style="color: #00AA00;">,</span> Geneva<span style="color: #00AA00;">,</span> Arial<span style="color: #00AA00;">,</span> Helvetica<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.rot1</span>
<span style="color: #00AA00;">&#123;</span>
  -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>1deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>1deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  rotation<span style="color: #00AA00;">:</span> 1deg<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.rot2</span>
<span style="color: #00AA00;">&#123;</span>
  -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>2deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>2deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  rotation<span style="color: #00AA00;">:</span> 2deg<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.rot3</span>
<span style="color: #00AA00;">&#123;</span>
  -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-1deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-1deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  rotation<span style="color: #00AA00;">:</span> -1deg<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.rot4</span>
<span style="color: #00AA00;">&#123;</span>
  -webkit-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-2deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>-2deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  rotation<span style="color: #00AA00;">:</span> -2deg<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></td></tr></table></div>

<p>As you can see, these styles use WebKit and Mozilla transforms so they&#8217;re not native to all browsers. The <code>rotation</code> attribute is also only available as part of CSS3, so again not all browsers will be using this.</p>
<p>The latest version of Firefox runs it fine, although I found problems with the latest Chrome and Safari strangley, even though they use WebKit. Basically, if the text doesn&#8217;t look slanted your browser doesn&#8217;t support rotation <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold-js-twbar.png"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/05/untold-js-twbar.png" alt="" title="untold-js-twbar" width="400" height="19" class="aligncenter size-full wp-image-554" /></a></p>
<p>And that&#8217;s it. I&#8217;ll be seeing how far I can go with these scripts into a pure Flash-less home page.</p>
<p>Next I&#8217;ll be adding to this Twitter bar a bit more and taking it from there.</p>
<p>I&#8217;d like to know what you think of this project. Should JavaScript be used as a preference over Flash for interface design? Are there parts of Ryan&#8217;s home page that can&#8217;t possibly be made in this way?</p>
<p>Let me know in the comments, or rant at me on <a href="http://twitter.com/_bigSteve">Twitter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/untold-entertainment-in-jquery-animating-twitter-text/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Messing with Background-Attachment</title>
		<link>http://stephenmcintyre.net/blog/messing-with-background-attachment/</link>
		<comments>http://stephenmcintyre.net/blog/messing-with-background-attachment/#comments</comments>
		<pubDate>Thu, 13 May 2010 21:17:00 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=489</guid>
		<description><![CDATA[
Earlier this week I came across an interesting Tumblr theme by Jarred Bishop. The site played with the background-attachment CSS property to show a unique background for each Tumblr post on his main page (depending on which post image he used) and stays static as you scroll.
I started to play around with some code to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/05/parrots.jpg"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/05/parrots.jpg" alt="" title="parrots" width="400" height="164" class="aligncenter size-full wp-image-500" /></a></p>
<p>Earlier this week I <a href="http://twitter.com/_bigSteve/status/13877279858">came across</a> an interesting Tumblr theme by <a href="http://jarredbishop.tumblr.com/">Jarred Bishop</a>. The site played with the <code>background-attachment</code> CSS property to show a unique background for each Tumblr post on his main page (depending on which post image he used) and stays static as you scroll.</p>
<p>I started to play around with some code to replicate this effect and instead came up with some different examples, while still manipulating <code>background-attachment</code>. Hit the jump for some demos.<span id="more-489"></span></p>
<p>Before we start coding, <a href="/examples/bg-attach/">check these out</a>!</p>
<p>Like them?</p>
<p>Moving the scroll bar on each box changes the effect in which the background moves inside it. You either get a simple scroller (nothing special), two garage-door-style shutters, or a really nice smooth-in animation that desaturates our parrot.</p>
<p>It&#8217;s all down to changing the <code>background-attachment</code> from &#8220;scroll&#8221; to &#8220;fixed&#8221;.</p>
<h3>Styling the Containers</h3>
<p>Here&#8217;s an example. This is from the last box in the set &#8211; the one that looks like it changes into black and white as you scroll. First we&#8217;ll set up the styles. These do most of the work.</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
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">&lt;style<span style="color: #00AA00;">&gt;</span>
  <span style="color: #00AA00;">*</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.container</span> div
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">auto</span><span style="color: #00AA00;">;</span>
    overflow-x<span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> 
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span> div
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #3333ff;">:no-</span>repeat<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'parrot.jpg'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span> div<span style="color: #6666ff;">.grey</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'parrot-grey.jpg'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
  <span style="color: #6666ff;">.container</span> div<span style="color: #6666ff;">.fixed</span>
  <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-attachment</span><span style="color: #00AA00;">:</span><span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
  <span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></td></tr></table></div>

<p>The first part sets all margins to zero (if you have a <code>&lt;body&gt;</code> tag it&#8217;s better to use that instead of the <code>*</code> &#8220;all&#8221; pseudo). This means we don&#8217;t get weird spacing issues with the background being in the top-left corner.</p>
<p>We set each container and the <code>&lt;div&gt;</code>s inside of them to have the same height and width, both matching your images. Means they won&#8217;t overlap each other with weird sizes.</p>
<p>The container class code gives the right side a scroll bar and removes it from the bottom (we don&#8217;t need the bottom one).</p>
<p>The rest should be okay.</p>
<h3>Adding the Containers</h3>
<p>There&#8217;s a tiny bit of HTML to add in.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td 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;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;container&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fixed&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;grey fixed&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>That&#8217;s all it takes, your CSS will do the rest.</p>
<p>We give our outer div the &#8220;container&#8221; class that we set earlier. The inside elements then can be given a &#8220;fixed&#8221; class to set its background-attachment.</p>
<p>Make sure to give the second one a &#8220;grey&#8221; class to change its image if you want to. For this you definitely do, but for the shutter-style examples it depends how you want it to look.</p>
<p>So now if you want to play around with the different effects, all you have to do is add or remove the &#8220;fixed&#8221; class from the HTML above.</p>
<p>Be sure to check the source of the example page to see how I did it. Just bear in mind that there are some styles in there that are purely to demonstrate them inside the grid.</p>
<p>Things like the &#8220;bottom&#8221; and &#8220;right&#8221; classes and the &#8220;position&#8221; attribute in the container class were only created for the example, but feel free to play with them and see what you get.</p>
<p>Tracing back to the start of this post, I&#8217;ll definitely be putting something else up based on Jarred&#8217;s theme, this being the first bit of insight into how it all works.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/messing-with-background-attachment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Load WordPress Gravatars on the Fly</title>
		<link>http://stephenmcintyre.net/blog/load-gravatars-on-the-fly/</link>
		<comments>http://stephenmcintyre.net/blog/load-gravatars-on-the-fly/#comments</comments>
		<pubDate>Thu, 06 May 2010 23:22:36 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[The Web]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=475</guid>
		<description><![CDATA[Globally Recognised Avatar (or Gravatar) is a service from the WordPress team that allows you to improve your web presence across blogs and specialist websites. It&#8217;s one of those &#8220;why didn&#8217;t I think of that?&#8221; products that are so simple but yet have so much opportunity to be integrated into virtually everything. You can see [...]]]></description>
			<content:encoded><![CDATA[<p>Globally Recognised Avatar (or <a href="http://gravatar.com" target="_blank">Gravatar</a>) is a service from the WordPress team that allows you to improve your web presence across blogs and specialist websites. It&#8217;s one of those &#8220;why didn&#8217;t I think of that?&#8221; products that are so simple but yet have so much opportunity to be integrated into virtually everything. You can see examples of this from the comments below and in my sidebar.</p>
<p>The problem with Gravatars that I find a lot of people mentioning is that they don&#8217;t get to see their Gravatar before they post a comment &#8211; like the <a href="http://www.shamusyoung.com/twentysidedtale/?p=1462#comments">comments section</a> on Shamus Young&#8217;s Wavatar post.</p>
<p>You may not think that&#8217;s too big of a deal, or perhaps from a marketing point of view that&#8217;s your way of driving comments (nasty people), but allowing them to play about with their image in the form will improve their relationship with your site and increase traffic anyway. Saves people deleting and re-commenting just because their Gravatar didn&#8217;t show up right too.</p>
<p>So this week I&#8217;ve put together a little script that generates these little Gravatars as you type. It&#8217;s even got little <a href="http://www.shamusyoung.com/twentysidedtale/?p=1462" target="_blank">Wavatar</a> faces on them that change when you make a new address, so you&#8217;ll never get bored!<span id="more-475"></span></p>
<p>Here&#8217;s the <a href="/examples/gravatar/">demo</a>.</p>
<p>So for this, we need some kind of an MD5 encryption algorithm, as the Gravatar API needs this to secure its e-mail addresses.</p>
<p>I&#8217;ve used a script from <a href="http://www.webtoolkit.info/javascript-md5.html">webtoolkit</a> that does the job perfectly.</p>
<p>The script&#8217;s over 200 lines long and you can find it <a href="/examples/gravatar/webtoolkit-md5.js">here</a>. I&#8217;ve included it in the script as an external file for easiness.</p>
<h2>Setting Up</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;gravatar&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;80&quot;</span> <span style="color: #000066;">alt</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Gravatar&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;webtoolkit-md5.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>Here we have our image, the input box, and the script tag that includes the MD5 script.</p>
<p>The Gravatar&#8217;s <code>src</code> will change when you start typing in the input field. It uses only a few lines of JavaScript.</p>
<h2>Working JavaScript&#8217;s Magic</h2>

<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;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> gravatar <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'gravatar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> email <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
email.<span style="color: #660066;">onkeyup</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #339933;">=</span> MD5<span style="color: #009900;">&#40;</span>email.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gravatar.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'http://www.gravatar.com/avatar/'</span><span style="color: #339933;">+</span>key<span style="color: #339933;">+</span><span style="color: #3366CC;">'?s=80&amp;d=wavatar'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>Lines 3 and 5</strong> fetch references to our image and input box to control them with the rest of our JavaScript.</p>
<p><strong>Line 7</strong> sets an <code>onkeyup</code> handler on the email field that is fired when a key is released.</p>
<p><strong>Line 8</strong> uses the webtoolkit MD5 algorithm and generates a key. We then pass this key into the Gravatar&#8217;s source and it shows our image. If it&#8217;s all worked out, this is where you should be seeing your own lovely face <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The quicker the user can interact with your application the better. This script allows you to generate Gravatar&#8217;s on the fly as the user types it, giving them results straight away.</p>
<p>For practice, try adding things like event timers to decrease the number of image changes at a time, or set the event to <code>onblur</code> so that it only ever fires when the focus is taken away from the field (good for comment forms).</p>
<p>And also, with their recent addition of Gravatar profiles that contain even more information, maybe &#8211; with a little more JavaScript &#8211; the email address will be all you ever need to type.</p>
<p>Happy coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/load-gravatars-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to Display a User&#8217;s Visited Web Pages</title>
		<link>http://stephenmcintyre.net/blog/display-users-visited-pages/</link>
		<comments>http://stephenmcintyre.net/blog/display-users-visited-pages/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 17:21:19 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=391</guid>
		<description><![CDATA[Allowing a developer to access a visitor&#8217;s browsing history is a huge security risk, that&#8217;s why it&#8217;s not possible. If you own a website that runs PHP with MySQL and handles sessions then this could be done by tracking IP addresses, user agents and so on to associate a browsing pattern with a particular person. [...]]]></description>
			<content:encoded><![CDATA[<p>Allowing a developer to access a visitor&#8217;s browsing history is a huge security risk, that&#8217;s why it&#8217;s not possible. If you own a website that runs PHP with MySQL and handles <a href="/blog/custom-php-session-class/">sessions</a> then this could be done by tracking IP addresses, user agents and so on to associate a browsing pattern with a particular person. This however can be pretty inaccurate, plus it&#8217;s only limited to your own website.</p>
<p>So what are we going to use? Well when you think about it, a user&#8217;s browsing history is stored on their computer, client-side. And what else runs client-side that us web developers can make? JavaScript and CSS. These are what we&#8217;re going to use.<span id="more-391"></span></p>
<p>Firstly, head on over to the <a href="/examples/visited/">visited demo page</a> to view what you&#8217;ll be making.</p>
<h3>The Page Links</h3>
<p>To start off we&#8217;ll set up our list of links:</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="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</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;btn-all&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>All<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;btn-visited&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Visited<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;btn-unvisited&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Unvisited<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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
<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;links&quot;</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/&quot;</span>&gt;</span>http://stephenmcintyre.net/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&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;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/blog/fragment-anchor-php/&quot;</span>&gt;</span>http://stephenmcintyre.net/blog/fragment-anchor-php/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&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;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/blog/kongregate-badge-feed-php/&quot;</span>&gt;</span>http://stephenmcintyre.net/blog/kongregate-badge-feed-php/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&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;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/blog/opening-new-windows-with-standards-ompliance/&quot;</span>&gt;</span>http://stephenmcintyre.net/blog/opening-new-windows-with-standards-ompliance/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&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;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/blog/guide-to-google-ajax-feed-api/&quot;</span>&gt;</span>http://stephenmcintyre.net/blog/guide-to-google-ajax-feed-api/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&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;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/blog/custom-php-session-class/&quot;</span>&gt;</span>http://stephenmcintyre.net/blog/custom-php-session-class/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&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;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/blog/let-your-database-do-it/&quot;</span>&gt;</span>http://stephenmcintyre.net/blog/let-your-database-do-it/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&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;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/blog/fetch-content-using-php/&quot;</span>&gt;</span>http://stephenmcintyre.net/blog/fetch-content-using-php/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&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;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/blog/addthis-api-social-bookmarks/&quot;</span>&gt;</span>http://stephenmcintyre.net/blog/addthis-api-social-bookmarks/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&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;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://stephenmcintyre.net/blog/jquery-parallax-scrolling/&quot;</span>&gt;</span>http://stephenmcintyre.net/blog/jquery-parallax-scrolling/<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>Here we have a list of article links from this website, and above them are some links to control which of the elements we will be showing. </p>
<p>Note the <code>&lt;br/&gt;</code> inside the <code>&lt;a&gt;</code> tags. These make them display properly when some elements are removed. I was going to use an unordered list, but this looked much nicer.</p>
<h3>Controlling the Page with JavaScript</h3>
<p>Now, to let those top links control our list, we&#8217;ll give them some JavaScript.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#btn-all'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#links'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'visited'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'unvisited'</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;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#btn-visited'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#links'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'unvisited'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'visited'</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;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#btn-unvisited'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#links'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'visited'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'unvisited'</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;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>The top line fetches jQuery from the Google code repository.</p>
<p>Each nested part of the jQuery code in the next <code>&lt;script&gt;</code> tag sets a &#8220;click&#8221; event for each top link, and removes or adds CSS classes to show each element properly.</p>
<p>Note that the classes only tell the browser which mode we are currently in and do not check whether the pages have actually been visited. With some clever CSS, we can properly display the links for this purpose.</p>
<p>This JavaScript snippet can go anywhere on the page, since this bit of jQuery will only run once the rest of the content has loaded.</p>
<h3>Visited and Unvisited Links in CSS</h3>
<p>This is the key in separating out our links. Remember to put this code at the top of your page (in the <code>&lt;head&gt;</code> if it has one).</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
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
a<span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
a<span style="color: #3333ff;">:hover</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span><span style="color: #993333;">underline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#btn-all</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#btn-unvisited</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#links</span> a<span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#06f</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#btn-visited</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#links</span> a<span style="color: #3333ff;">:visited</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#d5d</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#links</span><span style="color: #6666ff;">.unvisited</span> a<span style="color: #3333ff;">:visited</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#links</span><span style="color: #6666ff;">.visited</span> a<span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#links</span><span style="color: #6666ff;">.visited</span> a<span style="color: #3333ff;">:visited</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#links</span><span style="color: #6666ff;">.unvisited</span> a<span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span><span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></td></tr></table></div>

<p>The useful part here is the <code>:visited</code> pseudo selector, which tells your browser to take any elements linking to a page from your history and display them differently.</p>
<p>Take lines 17-19 for example. Clicking &#8220;Unvisited&#8221; will set the <code>links</code> div element to have an <code>unvisited</code> class. Thanks to the <code>:visited</code> selector and setting the style to <code>display:none</code>, this means that any links that you have visited will not be shown. This is what we want.</p>
<p>Also, if &#8220;Visited&#8221; has been clicked, all links will be hidden. The style below these lines then resets all visited links so that only those are shown.</p>
<p>Don&#8217;t worry if it seems a bit confusing, just paste the source from the <a href="/examples/visited/">visited demo page</a> into an HTML editor and have a play around with it, see where it takes you.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/display-users-visited-pages/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Create Social Bookmarks with the AddThis API</title>
		<link>http://stephenmcintyre.net/blog/addthis-api-social-bookmarks/</link>
		<comments>http://stephenmcintyre.net/blog/addthis-api-social-bookmarks/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 21:05:02 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[The Web]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=361</guid>
		<description><![CDATA[One service that I had used a while back was AddThis social bookmarks. I really liked its ease of use, support for a huge range of social networks and bookmarking sites, and most importantly it had great tools for analytics.
The social bar itself however left more to be desired. It was an eyesore, appearing after [...]]]></description>
			<content:encoded><![CDATA[<p>One service that I had used a while back was <a href="http://www.addthis.com">AddThis</a> social bookmarks. I really liked its ease of use, support for a huge range of <strong>social networks</strong> and <strong>bookmarking</strong> sites, and most importantly it had great tools for <strong>analytics</strong>.</p>
<p>The social bar itself however left more to be desired. It was an eyesore, appearing after every post on my site, and made me cringe at the fact that it <strong>relied on JavaScript</strong> to function. Luckily, I found an alternative in the form of their API docs.<span id="more-361"></span></p>
<p>You can see this for yourself in the <a href="#share">social bar</a> below this post. Check out the links as you hover over the images, they&#8217;re constructed exactly as I will show you here.</p>
<h3>Finding the Link</h3>
<p>The trick here is as simple as tweaking a URL.</p>
<p><code>http://api.addthis.com/oexchange/0.8/offer?url=<strong>URL</strong></code></p>
<p>This is the most basic option of the API. It sends us to a page in which we can <strong>choose</strong> where we want to share the link. It&#8217;s good if you have a simple &#8220;Share This&#8221; button, but not if we want different links to specific social networks. Plus, we won&#8217;t get that analytics data.</p>
<p>In the above example, just replace <code>URL</code> with your page&#8217;s location.</p>
<p><a href="http://api.addthis.com/oexchange/0.8/offer?url=http://stephenmcintyre.net">http://api.addthis.com/oexchange/0.8/offer?url=<strong>http://stephenmcintyre.net</strong></a></p>
<p>Now we&#8217;ll add a title and user name. For this we add another two URL parameters. Just replace URL, title and user name like before.</p>
<p><a href="http://api.addthis.com/oexchange/0.8/offer?url=http://stephenmcintyre.net&#038;title=Stephen+McIntyre&#038;username=StephenMcIntyre">http://api.addthis.com/oexchange/0.8/offer?url=<strong>http://stephenmcintyre.net</strong>&amp;title=<strong>Stephen+McIntyre</strong>&amp;username=<strong>StephenMcIntyre</strong></a></p>
<p>And lastly, where we want to specify where we are going to take the user we need to add <code>forward/SERVICE/</code> into our link.</p>
<p><a href="http://api.addthis.com/oexchange/0.8/forward/facebook/offer?url=http://stephenmcintyre.net&#038;title=Stephen+McIntyre&#038;username=StephenMcIntyre">http://api.addthis.com/oexchange/0.8/forward/<strong>facebook</strong>/offer?url=<strong>http://stephenmcintyre.net</strong>&amp;title=<strong>Stephen+McIntyre</strong>&amp;username=<strong>StephenMcIntyre</strong></a></p>
<p>The link above will take you straight to Facebook, formatting the data to easily post the page up onto your wall.</p>
<h3>A Simple Example</h3>
<p>So, for a full implementation of this API we can use a list of hyperlinks like in my social bar, and styled how you like them.</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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.addthis.com/oexchange/0.8/forward/twitter/offer?url=http%3A%2F%2Fstephenmcintyre.net&amp;amp;title=Stephen+McIntyre&amp;amp;username=StephenMcIntyre&quot;</span>&gt;</span>Twitter<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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.addthis.com/oexchange/0.8/forward/facebook/offer?url=http%3A%2F%2Fstephenmcintyre.net&amp;amp;title=Stephen+McIntyre&amp;amp;username=StephenMcIntyre&quot;</span>&gt;</span>Facebook<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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.addthis.com/oexchange/0.8/forward/digg/offer?url=http%3A%2F%2Fstephenmcintyre.net&amp;amp;title=Stephen+McIntyre&amp;amp;username=StephenMcIntyre&quot;</span>&gt;</span>Digg<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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://api.addthis.com/oexchange/0.8/forward/delicious/offer?url=http%3A%2F%2Fstephenmcintyre.net&amp;amp;title=Stephen+McIntyre&amp;amp;username=StephenMcIntyre&quot;</span>&gt;</span>Delicious<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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></pre></td></tr></table></div>

<p>For a full list of service tags to use in the <code>SERVICE</code> section of the URL, check out the <a href="http://www.addthis.com/services/list">service codes</a> page on the AddThis docs. You can copy the short codes from each row, there&#8217;s loads to choose from.</p>
<p>More information on the <a href="http://www.addthis.com/help/sharing-api">sharing endpoints</a> can also be found on there, which will help if you&#8217;re looking to dig further into the API.</p>
<h3>One Last Thing</h3>
<p>So as you can see, the links can get pretty long. If you&#8217;re inputting these manually, even a copy-paste job can be tedious, especially if you plan to go about dotting them all over your website.</p>
<p>The ideal situation then would be to generate these automatically, which can be done through PHP with some <a href="http://php.net/urlencode">urlencode</a> magic.</p>
<p>Also, if you&#8217;re using WordPress, then fetching the link and title is easy with <code>get_permalink()</code> and <code>$post-&gt;post_title</code> respectively.</p>
<p>I&#8217;ll be releasing a tool that utilises the AddThis API very soon, and adding some tutorials on here as to how it&#8217;s done, including some clever PHP.</p>
<p>Stay posted. <img src='http://stephenmcintyre.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/addthis-api-social-bookmarks/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Beginner&#8217;s Guide to Google AJAX Feed API</title>
		<link>http://stephenmcintyre.net/blog/guide-to-google-ajax-feed-api/</link>
		<comments>http://stephenmcintyre.net/blog/guide-to-google-ajax-feed-api/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 21:49:06 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=328</guid>
		<description><![CDATA[For those that don&#8217;t know, the Google AJAX Feed API is a JavaScript tool that you can use to handle your AJAX requests, and is particularly useful for reading in feed data from external websites. After searching for all kinds of plug-ins and tools, I landed on Google&#8217;s answer, and found exactly what I needed [...]]]></description>
			<content:encoded><![CDATA[<p>For those that don&#8217;t know, the <a href="http://code.google.com/apis/ajaxfeeds/">Google AJAX Feed API</a> is a JavaScript tool that you can use to handle your AJAX requests, and is particularly useful for reading in feed data from external websites. After searching for all kinds of plug-ins and tools, I landed on Google&#8217;s answer, and found exactly what I needed &#8211; a JavaScript-only feed parsing script.</p>
<p>This tutorial will provide you with a very basic script to start using this great tool and covers what you need to get started.</p>
<p><span id="more-328"></span></p>
<p>First off, check out the <a href="/examples/gaf-api/">Feed API demo</a> that you will be creating.</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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;ul id=&quot;container&quot;&gt;&lt;/ul&gt;
&nbsp;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.google.com/jsapi?key=YOUR_API_KEY&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> undefined<span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> items <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;container&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> Initialize<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> feed <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">feeds</span>.<span style="color: #660066;">Feed</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://twitter.com/statuses/user_timeline/41298392.rss&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	feed.<span style="color: #660066;">setNumEntries</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	feed.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<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><span style="color: #339933;">!</span>result.<span style="color: #660066;">error</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			items <span style="color: #339933;">=</span> result.<span style="color: #660066;">feed</span>.<span style="color: #660066;">entries</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>
<span style="color: #009900;">&#125;</span>
google.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;feeds&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
google.<span style="color: #660066;">setOnLoadCallback</span><span style="color: #009900;">&#40;</span>Initialize<span style="color: #009900;">&#41;</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>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> undefined<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #003366; font-weight: bold;">var</span> ipop <span style="color: #339933;">=</span> items.<span style="color: #660066;">pop</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> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;li&gt;'</span> <span style="color: #339933;">+</span> ipop.<span style="color: #660066;">title</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/li&gt;'</span><span style="color: #339933;">;</span>
		container.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+=</span> text<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>If it seems overwhelming, don&#8217;t worry. I&#8217;ll go over it bit by bit, it&#8217;s really easy to pick up.</p>
<h3>Initialize</h3>
<p>This is a custom method that takes in some data using the code from the first <code>&lt;script&gt;</code> tag.</p>
<p>The first line inside of this function declaration stores feed data into the <code>feed</code> object. Setting the parameter in the <code>Feed</code> method call will change what RSS data you use.</p>
<p>Line two sets how many items you want it to display, and the final section tells the script what to do once the data has been loaded into the variable. In this case, we want it to check that there are no errors and store our list of entries into the <code>items</code> variable.</p>
<h3>load and setOnLoadCallback</h3>
<p>The <code>load</code> method tells the Google API that we want to use the Feed functions, so we give it the parameters &#8220;feed&#8221; and &#8220;1&#8243;. The &#8220;1&#8243; part is only used for collecting version numbers and isn&#8217;t too important.</p>
<p><code>setOnLoadCallback</code> calls our <code>Initialize</code> method from earlier and processes it.</p>
<h3>The fancy setInterval stuff</h3>
<p>This section is just for eye candy and isn&#8217;t actually essential to using the API. It&#8217;s used to make the feed items display a second after each other in a list.</p>
<p>To start off we check if the item can be used, by checking if it isn&#8217;t undefined. We then remove an item from our list of items with <code>pop</code> and store this into <code>ipop</code>. The <code>title</code> attribute of this is then used as the text for the list item and added to our <code>container</code> list.</p>
<h3>Finishing up</h3>
<p>There we go, hopefully as simple as I said it would be.</p>
<p>Just remember that before you use this script, you should sign up for an <a href="http://code.google.com/apis/ajaxfeeds/signup.html">API key</a> and replace YOUR_API_KEY in the script tag with the key you&#8217;re given.</p>
<p>What I really like about this script is that it&#8217;s all handled (on our end at least) by one HTML document, and Google&#8217;s speedy servers handle the rest of it for us.</p>
<p>This is not a service to be abused however, as it has huge possibilities and there are millions of ways that applications could be built using it. With the issue of bandwidth and server load costs affecting a lot of amateur developers with big ideas, this is one less thing to worry about.</p>
<h3>Note</h3>
<p>The code above may only work if you have a properly formatted HTML document (i.e., all the header, doctype and body stuff). If you do have problems, try copying the code from the demo&#8217;s page source.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/guide-to-google-ajax-feed-api/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Using jQuery To Create Parallax Scrolling Backgrounds</title>
		<link>http://stephenmcintyre.net/blog/jquery-parallax-scrolling/</link>
		<comments>http://stephenmcintyre.net/blog/jquery-parallax-scrolling/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 22:43:00 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=309</guid>
		<description><![CDATA[Just a quick demonstration today on using JavaScript and CSS techniques to create scrolling backgrounds. Here we layer backgrounds on top of each other and change their horizontal position to move them past the viewing area. It also uses different multipliers to move the closer objects quicker, and create a parallax effect.


After you&#8217;ve checked out [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick demonstration today on using JavaScript and CSS techniques to create scrolling backgrounds. Here we layer backgrounds on top of each other and change their horizontal position to move them past the viewing area. It also uses different multipliers to move the closer objects quicker, and create a <strong>parallax</strong> effect.</p>
<p><a href="http://stephenmcintyre.net/wp-content/uploads/2010/02/parallax.jpg"><img src="http://stephenmcintyre.net/wp-content/uploads/2010/02/parallax-300x108.jpg" alt="" title="parallax" width="300" height="108" class="aligncenter size-medium wp-image-321" /></a></p>
<p><span id="more-309"></span></p>
<p>After you&#8217;ve checked out the <a href="/examples/parallax/">parallax scrolling demo</a> you can head back here to see how it&#8217;s written up.</p>
<p>I used jQuery for rapid prototyping, but naturally you can use regular JavaScript if you know the tricks to do so.</p>
<h3>The Styles</h3>

<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="css" style="font-family:monospace;"><span style="color: #cc00cc;">#landscape</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#grass</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#verge</span><span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#window</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">460px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">332px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">background-repeat</span><span style="color: #3333ff;">:repeat-</span>x<span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">background-position</span><span style="color: #00AA00;">:</span><span style="color: #933;">0px</span> <span style="color: #933;"><span style="color: #cc66cc;">100</span>%</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#landscape</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'landscape.jpg'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#grass</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'grass.png'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#verge</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'verge.png'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#window</span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">background-image</span><span style="color: #00AA00;">:</span><span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'window.png'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>The first section sets height, width, background position and background repeat of the <code>&lt;div&gt;</code> elements that hold our backgrounds.</p>
<p>The other ID-specific sections set the background image you want to use for each part.</p>
<p>All CSS can be tweaked to suit your own project or how you see fit.</p>
<h3>The Structure</h3>

<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="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;landscape&quot;</span>&gt;</span>
	<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;grass&quot;</span>&gt;</span>
        <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;verge&quot;</span>&gt;</span>
            <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;window&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>All just divs inside divs here. Now, the interesting part.</p>
<h3>The Scripts</h3>

<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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> xAdd <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> scrollInterval <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>
		xAdd<span style="color: #339933;">++;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>xAdd <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">920</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			xAdd <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#landscape'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-position'</span><span style="color: #339933;">,</span>xAdd <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px 100%'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#grass'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-position'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span>xAdd <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px 100%'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#verge'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background-position'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</span>xAdd <span style="color: #339933;">*</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px 100%'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">10</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: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>First line imports our jQuery file and does the usual running of it in the next script tag.</p>
<p>We then set <code>xAdd = 0</code> &#8211; this will act as a <strong>counter</strong> to add to each background&#8217;s <strong>x-position</strong> and move the backgrounds horizontally behind the window.</p>
<p>Use the <code>scrollInterval</code> variable and <code>setInterval</code> method to set a function that calls every 10ms here. It&#8217;s a type of <strong>timer</strong> that will increment our <code>xAdd</code> every time it is called (10ms).</p>
<p>The if statement is a sort of <strong>garbage collector</strong>, resetting our number to 0 if it&#8217;s more than or equal to 920 (the maximum width of our images). It won&#8217;t break if you remove it, but it stops the number becoming unnecessarily high. Always good to keep these things in mind while you code.</p>
<p>The following three lines with the $ signs at the beginning allow jQuery to <strong>select</strong> our three div elements with the backgrounds in them. It then uses the <code>.css()</code> method to change their background position and creates the scrolling effect you see, since this is called every time the interval is run. There are also some multipliers on the xAdd variable in each one as mentioned earlier to speed up the <strong>closest</strong> objects.</p>
<p>So yeah, have fun with it. Just a little idea I had. Perhaps you can make better use of it than a simulation of a train journey daydream.</p>
<p>I dunno, surprise me. Let&#8217;s see what can be made of parallax scrolling in web pages. Interested to try this in a page header&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/jquery-parallax-scrolling/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Open Windows in HTML with Standards Compliance</title>
		<link>http://stephenmcintyre.net/blog/opening-new-windows-with-standards-ompliance/</link>
		<comments>http://stephenmcintyre.net/blog/opening-new-windows-with-standards-ompliance/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 21:43:17 +0000</pubDate>
		<dc:creator>Stephen McIntyre</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XHTML]]></category>

		<guid isPermaLink="false">http://stephenmcintyre.net/?p=299</guid>
		<description><![CDATA[When opening new windows from your web page that link to external files, a common practice is to open them in a new window as not to deter the user from your site.
One method to do this in the past was to use target="_blank" that tells the browser to open the link&#8217;s href attribute in [...]]]></description>
			<content:encoded><![CDATA[<p>When opening new windows from your web page that link to external files, a common practice is to open them in a new window as not to deter the user from your site.</p>
<p>One method to do this in the past was to use <code>target="_blank"</code> that tells the browser to open the link&#8217;s <code>href</code> attribute in a new window. This, however, is deprecated in the Strict Doctype of <a href="http://w3.org">W3C web standards</a> so we should be trying to avoid using it whenever we can, and in this post I&#8217;ll show you exactly how it&#8217;s done.</p>
<p><span id="more-299"></span></p>
<p>Instead of using the <code>target</code> attribute, we can use <code>rel</code> to define the <code>&lt;a&gt;</code> tags in our HTML that we want to open in a new page. Since these are usually external pages, we will use the <code>external</code> keyword.</p>
<p><a href="/examples/replace-target-blank/">Raw JavaScript Example</a></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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Open a &lt;a href=&quot;new-window.html&quot; rel=&quot;external&quot;&gt;New Window&lt;/a&gt; with W3C valid syntax
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> links <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'a'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> links.<span style="color: #660066;">length</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> rels <span style="color: #339933;">=</span> links<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">rel</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> rels.<span style="color: #660066;">length</span> <span style="color: #339933;">;</span> j<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> rels<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'external'</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			links<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
				window.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<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>
&nbsp;
				<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><strong>Line 1:</strong> This is where we give the link a <code>rel</code> attribute<br />
<strong>Lines 5-13:</strong> The script collects all <code>&lt;a&gt;</code> tags and loops through them, splitting up each space separated term and checking if they equal &#8220;<code>external</code>&#8221;<br />
<strong>Lines 15-17:</strong> Sets the <code>onclick</code> attribute of the link to allow <code>window.open</code> to run and open a new window when the user clicks, containing the <code>href</code> value.<br />
<strong>Line 19:</strong> Returns <code>false</code> to override the link&#8217;s default function, stopping it from opening a page of its own in the same window.</p>
<p>This can be run on any page that you have set your external links on and acts as a nicer, search-bot and standards friendly alternative.</p>
<p><strong>jQuery Version</strong></p>
<p>If your page is already using <a href="http://jquery.com">jQuery</a> there is an even easier way to implement the code from above.</p>
<p><a href="/examples/replace-target-blank/jquery-alt.html">jQuery Example</a></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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Open a &lt;a href=&quot;new-window.html&quot; rel=&quot;external&quot;&gt;New Window&lt;/a&gt; with W3C valid syntax
&nbsp;
&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'a[rel~=external]'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	window.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span>.<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>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Much simpler.</p>
<p>I won&#8217;t go through the code on the jQuery example, but if you use the library, you should understand the selectors and methods I&#8217;ve used above.</p>
<p>The clever part is on line 7, where the selector finds all <code>&lt;a&gt;</code> tags with a <code>rel</code> attribute containing the &#8220;<code>external</code>&#8221; keyword, even within a space-separated list of words.</p>
]]></content:encoded>
			<wfw:commentRss>http://stephenmcintyre.net/blog/opening-new-windows-with-standards-ompliance/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

