<?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>Paul Hayes &#187; javascript</title>
	<atom:link href="http://www.paulrhayes.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paulrhayes.com</link>
	<description>The web musings and experiments of.</description>
	<lastBuildDate>Sun, 06 Nov 2011 11:31:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Animated CSS3 cube using 3D transforms</title>
		<link>http://www.paulrhayes.com/2009-07/animated-css3-cube-interface-using-3d-transforms/</link>
		<comments>http://www.paulrhayes.com/2009-07/animated-css3-cube-interface-using-3d-transforms/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 01:16:54 +0000</pubDate>
		<dc:creator>Paul Hayes</dc:creator>
				<category><![CDATA[Experiments]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[perspective]]></category>
		<category><![CDATA[transformations]]></category>
		<category><![CDATA[transitions]]></category>
		<category><![CDATA[webkit]]></category>

		<guid isPermaLink="false">http://www.fofronline.com/?p=99</guid>
		<description><![CDATA[Position elements with 3D transforms to build a cube and use transitions to rotate on keypress.]]></description>
			<content:encoded><![CDATA[<p class="update"><strong>Update (28th Sept 2010)</strong>: Cube updated to work with touch gestures and click and drag — <a href="http://www.paulrhayes.com/2010-09/3d-css-cube-ii-touch-gestures-click-and-drag/">3D CSS cube II</a></p>
<p>Last week WebKit included the much anticipated (at least on my part) 3D transforms in its latest nightly build, announced practically alongside the awesome <a href="http://www.satine.org/research/webkit/snowleopard/snowstack.html">Snow Stack demo</a> that provides a 3D interface for browsing Flickr images (use left, right and space-bar). Today the <a href="http://webkit.org/blog/386/3d-transforms/">Surfin Safari blog</a> has updated with some more exciting demos, including “<em>Morphin Power Cubes</em>” and “<em>Poster Circle</em>”. It is now possible to create all sorts of crazy three-dimensional and animated user interfaces; the power comes largely in <code>-webkit-perspective</code> and a number of updated transforms–adapted to incorporate the Z axis.</p>
<p><span id="more-99"></span></p>
<p>Since working on the <a href="/2009-04/3d-cube-using-css-transformations/">3D cube using 2D transforms</a> back in April I’ve experimented with perspective to create something more powerful, playing around with 3D transforms on the iPhone a few times (e.g. this <a href="/experiments/perspective/">early rotating demo</a>). Now I’ve got something worth sharing.</p>
<p>A 3D cube can be created solely in CSS, with all six faces. Using JavaScript to detect key presses and update inline styles this cube can be intuitively navigated.</p>
<h3>Result</h3>
<p><a href="/experiments/cube-3d/index.html">A 3D cube that rotates using the Up, Down, Left and Right arrow keys.</a><br />
Supported browsers: WebKit Nightly r46042+</p>
<p class="center"><img src="http://host.trivialbeing.org/up/fofr-online-20090717-3d-cube.jpg" alt="3D cube interface using new WebKit transforms" /></p>
<h3>How To</h3>
<p>I’ll start with the markup, because it’s simple. Each of the six cube faces is given a face class and another relating to it’s number. These six faces sit within a cube container, which sits in another wrapper, each is necessary.</p>
<pre class='prettyprint'>&lt;div id=&quot;experiment&quot;&gt;
	&lt;div id=&quot;cube&quot;&gt;
		&lt;div class=&quot;face one&quot;&gt;
			One face
		&lt;/div&gt;
		&lt;div class=&quot;face two&quot;&gt;
			Up, down, left, right
		&lt;/div&gt;
		&lt;div class=&quot;face three&quot;&gt;
			Lorem ipsum.
		&lt;/div&gt;
		&lt;div class=&quot;face four&quot;&gt;
			New forms of navigation are fun.
		&lt;/div&gt;
		&lt;div class=&quot;face five&quot;&gt;
			Rotating 3D cube
		&lt;/div&gt;
		&lt;div class=&quot;face six&quot;&gt;
			More content
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/div&gt;</pre>
<p>The outer wrapper serves as a camera, on which you apply some perspective — appropriate 3D transformations are then applied to descendants. <code>-webkit-perspective</code> defines the depth of the Z-plane and relative sizes of elements above and below it, <code>-webkit-perspective-origin</code> specifies the perspective’s origin. <a href="http://webkit.org/blog-files/3d-transforms/perspective-by-example.html">View a perspective example (webkit.org)</a></p>
<pre class='prettyprint'>
    #experiment {
      -webkit-perspective: 800;
      -webkit-perspective-origin: 50% 200px;
    }
</pre>
<p>The second container, the actual cube, has a specified height, margin, position, etc. as usual. The height and width are necessary to create some confines for the cube face transformations — alternatively the width defaults to 100% and the cube’s appearance would vary with window width. <code>-webkit-transition</code> (<a href="http://www.w3.org/TR/css3-transitions/">documentation</a>) defines the animated property, duration and timing-function — we’re animating the 3d transformation (via <code>-webkit-transform</code>) linearly for two seconds. <code>-webkit-transform-style</code> determines whether child elements lie flat against their parent (“flat”) or remain in 3D space (“preserve-3d”).</p>
<pre class='prettyprint'>
    #cube {
      position: relative;
      margin: 0 auto;
      height: 400px;
      width: 400px;
      -webkit-transition: -webkit-transform 2s linear;
      -webkit-transform-style: preserve-3d;
    }
</pre>
<p>Using the <code>.face</code> class common styles are applied to the six sides; coloring, size, padding, etc. Importantly they are each positioned absolutely, relative to the cube container. The background rgba property is included to make the cube look pretty and transparent.</p>
<pre class='prettyprint'>
    .face {
      position: absolute;
      height: 360px;
      width: 360px;
      padding: 20px;
      background-color: rgba(50, 50, 50, 0.7);
    }
</pre>
<p>Each of the faces, one through six, needs to be rotated in 3D space to its correct starting position. Using <code>translateZ</code> the elements are brought 200px (half their width) off the Z-plane. Each of the faces must be at 90 degrees. Rotating solely in the X plane positions the top and bottom faces (one, six), before rotating the last four faces in the Y plane, much like origami. The extra rotate on the sixth face rotates the content in 2D space to correct its orientation.</p>
<pre class='prettyprint'>
    #cube .one  {
      -webkit-transform: rotateX(90deg) translateZ(200px);
    }

    #cube .two {
      -webkit-transform: translateZ(200px);
    }

    #cube .three {
      -webkit-transform: rotateY(90deg) translateZ(200px);
    }

    #cube .four {
      -webkit-transform: rotateY(180deg) translateZ(200px);
    }

    #cube .five {
      -webkit-transform: rotateY(-90deg) translateZ(200px);
    }

    #cube .six {
      -webkit-transform: rotateX(-90deg) translateZ(200px) rotate(180deg);
    }
</pre>
<p>Our cube is now complete — but it doesn’t move! With a keydown event listener we can increment X and Y angles based on different key presses, before applying them as inline styles on the cube container. In combination with the transition effect on #cube, all six faces rotate in sync from their original position to the newly defined angle, creating a seamless 3D cube interface.</p>
<pre class='prettyprint'>
  	var xAngle = 0, yAngle = 0;
	document.addEventListener('keydown', function(e)
	{
		switch(e.keyCode)
		{

			case 37: // left
				yAngle -= 90;
				break;

			case 38: // up
				xAngle += 90;
				break;

			case 39: // right
				yAngle += 90;
				break;

			case 40: // down
				xAngle -= 90;
				break;
		};

		$('cube').style.webkitTransform = "rotateX("+xAngle+"deg) rotateY("+yAngle+"deg)";
	}, false);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.paulrhayes.com/2009-07/animated-css3-cube-interface-using-3d-transforms/feed/</wfw:commentRss>
		<slash:comments>74</slash:comments>
		</item>
		<item>
		<title>Moving markup towards HTML5</title>
		<link>http://www.paulrhayes.com/2009-06/moving-markup-towards-html5/</link>
		<comments>http://www.paulrhayes.com/2009-06/moving-markup-towards-html5/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 23:03:31 +0000</pubDate>
		<dc:creator>Paul Hayes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.fofronline.com/?p=78</guid>
		<description><![CDATA[A foray into marking up a blog with HTML5]]></description>
			<content:encoded><![CDATA[<p>Having read John Resig’s “<a href="http://ejohn.org/blog/html5-shiv/">HTML5 Shiv</a>” article and Remy Sharp’s “<a href="http://remysharp.com/2009/01/07/html5-enabling-script/">HTML5 enabling script</a>”, it felt like the right time to begin the full fledged migration from XHTML to a cross browser compatible HTML5 blog. All in all the process of updating the templates was painless, taking about an hour or so to modify the WordPress Sandbox theme.<br />
<span id="more-78"></span></p>
<p>To enable IE6 and IE7 support for new HTML5 tags, which are not naturally styled, some JavaScript is necessary. As per the ‘shiv’ article, Remy Sharp has a small script that creates DOM elements, one for each type of new HTML5 tag, the simple act of doing so leads Internet Explorer to apply styles to said tags. I slightly modified the existing script to add the recently proposed <code>hgroup</code>.</p>
<p>Even though these tags accept style they don’t come with their default renderings. For that we need a bit of CSS to make block elements behave as they should.</p>
<pre class='prettyprint'>
(function(){
	if(!/*@cc_on!@*/0) return;
	var e = &quot;abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,
		eventsource,figure,footer,hgroup,header,mark,menu,meter,nav,output,
		progress,section,time,video&quot;.split(','),i=0,length=e.length;
	while(i&lt;length){
		document.createElement(e[i++])
	}
})();
</pre>
<pre class='prettyprint'>
article, aside, dialog, footer, header, section, footer, nav, figure {
	display: block;
}
</pre>
<p>I’ve also updated the Eric Meyer reset script, removing now deprecated HTML 4 tags and applying reset to the new elements, so they do not unexpectedly inherit padding, margin, etc. in the future. These changes are not yet exhaustive.</p>
<p>Moving onto the page’s actual markup, the new DOCTYPE and character encoding settings are remarkably simple. Standards based web development is getting easier. For browsers that do not support HTML5, the new DOCTYPE still triggers standards mode. The <code>xmlns</code> HTML attribute is no longer necessary and the <code>profile</code> attribute has been dropped.</p>
<pre class='prettyprint'>
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;
&lt;head&gt;
	&lt;meta charset=&quot;UTF-8&quot; /&gt;
	&lt;title&gt;FofR Online&lt;/title&gt;
</pre>
<p>The header section has been placed in the appropriate <code>&lt;header&gt;</code> tags, and similarly with the footer. I’d hoped to include the ‘About Me’ section within this, but as part of the specification you cannot include headings within a <code>&lt;footer&gt;</code> element.</p>
<p>Each of the posts comes wrapped in an <code>&lt;article&gt;</code> tag, i.e. an independent element with content that could standalone. Within are the respective <code>&lt;header&gt;</code> (containing title and date) and <code>&lt;footer&gt;</code> (containing meta links)  elements. Technically the meta links could be marked as <code>&lt;nav&gt;</code>, but the former is more fitting and still acceptable use.</p>
<p>The date makes use of HTML5’s <code>&lt;time&gt;</code> element, with a <code>datetime</code> attribute that gives the precise posting time, including timezone offset.</p>
<p>The previous and next links that follow the article can comfortably sit within a <code>&lt;nav&gt;</code> tag. Similarly, my sidebar region is predominantly navigation based with lists of archives and categories, it’s been marked as such.</p>
<pre class='prettyprint'>
&lt;article id=&quot;post-67&quot; class=&quot;&quot;&gt;
	&lt;header&gt;
		&lt;h2 class=&quot;entry-title&quot;&gt;&lt;a href=&quot;&quot; title=&quot;&quot; rel=&quot;bookmark&quot;&gt;POST TITLE&lt;/a&gt;&lt;/h2&gt;
		&lt;div class=&quot;entry-date&quot;&gt;
			&lt;time datetime=&quot;2009-04-30T15:54:28-07:00&quot; class=&quot;published&quot; title=&quot;2009-04-30T15:54:28-07:00&quot;&gt;April 30, 2009 &amp;#8211; 3:54 pm&lt;/time&gt;
		&lt;/div&gt;
	&lt;/header&gt;
	&lt;div class=&quot;entry-content&quot;&gt;
		POST
	&lt;/div&gt;
	&lt;footer class=&quot;entry-meta&quot;&gt;
		META LINKS
	&lt;/footer&gt;
&lt;/article&gt;

&lt;nav id=&quot;nav-below&quot; class=&quot;navigation clearfix&quot;&gt;
	&lt;div class=&quot;nav-previous&quot;&gt;&lt;/div&gt;
	&lt;div class=&quot;nav-next&quot;&gt;&lt;/div&gt;
&lt;/nav&gt;
</pre>
<p>One avenue I should explore is the inclusion of the <code>&lt;section&gt;</code> tag, which I’d like to break up individual posts, probably by splitting the content at level three headings downwards; thereby becoming the header of each new section.</p>
<p>It’ll be a while before the real benefits of HTML5 can be fully appreciated by everyone, but it feels good to make a start, however small that step may be.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paulrhayes.com/2009-06/moving-markup-towards-html5/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Track outbound links using Google Analytics</title>
		<link>http://www.paulrhayes.com/2009-03/track-outbound-links-using-google-analytics/</link>
		<comments>http://www.paulrhayes.com/2009-03/track-outbound-links-using-google-analytics/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 00:06:20 +0000</pubDate>
		<dc:creator>Paul Hayes</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[analytics]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.fofronline.com/?p=14</guid>
		<description><![CDATA[Combine CSS3 selectors and analytics event tracking to report on outbound links.]]></description>
			<content:encoded><![CDATA[<p>“Track everything”, lest vital visitor trends fall through the cracks — that’s my newly endorsed web analytics doctrine. As a precursor to the quantitative ‘what’ and the qualitative ‘why’ we need that cold hard data before analysis can begin; Google Analytics is the popular harvester of choice and out of the box it grabs a lot. Visits, Pageviews, Screen resolution, et al — <acronym title="Google Analytics">GA</acronym> seemingly has all your conventional data needs covered. But one significant trend is lacking — how visitors leave your site, specifically through outbound links on a page, data that inevitably leads to a what and an avenue for investigating the why. For instance, “<em>Which partner sites are attracting the highest click throughs?</em>” or more generally “<em>Why do visitors leave my site?</em>”.<br />
<span id="more-14"></span></p>
<p>GA gives the ability to <a href="http://code.google.com/apis/analytics/docs/eventTrackerGuide.html">create your own events</a> with a category, action, label and numerical value using the syntax:</p>
<pre class="thin">_trackEvent(category, action, optional_label, optional_value)</pre>
<p>Hence, on an outbound link click, by calling this JavaScript method you can trigger a tracked event in GA. An obtrusive onclick attribute on every outbound link is both cumbersome to implement and difficult to manage, it also goes against the best practices of progressive enhancement and unobtrusiveness.</p>
<p>The solution is to attach a click event listener to each of the outbound links on the page, and the question becomes how to do that. CSS3 comes with a couple of handy <a href="http://www.w3.org/TR/css3-selectors/">new selectors</a> that we can use in combination with Prototype or jQuery to root out the correct links. The appropriate selectors:</p>
<blockquote><p>
E[foo^=“bar”]  	an E element whose “foo” attribute value begins exactly with the string “bar“<br />
E[foo*=“bar”] 	an E element whose “foo” attribute value contains the substring “bar“<br />
E:not(s)  	an E element that does not match simple selector s
</p></blockquote>
<p>The magic outbound link selector then becomes one of the following, depending on your needs:</p>
<pre class='prettyprint'>
/* Any link that does not contain yourdomain.com */
a:not(a[href*="yourdomain.com"])

/* Any link that does not start with yourdomain.com */
a:not(a[href^="yourdomain.com"])

/* Any link that does not start with yourdomain.com or www.yourdomain.com */
a:not(a[href^="yourdomain.com"]):not(a[href^="www.yourdomain.com"])

/* Any link that starts with http - e.g. any non relative links */
a[href^="http"]

/* Catch all - any link that starts with http but doesn't link to your domain */
a[href^="http"]:not(a[href*="yourdomain.com"])
</pre>
<p>With an array of all the outbound links at hand, adding a click listener is simple. But we do need to set up the category, action and label. I have opted to create an arbitrary “Outbound Link” category that uses the link’s text (with HTML tags stripped out) as the action and the url as the label:</p>
<pre class='prettyprint'>
Event.observe(outboundLink, 'click', function() {
        // category, action, label
        pageTracker._trackEvent('Outbound Link', outboundLink.innerHTML.replace(/(&lt;([^&gt;]+)&gt;)/ig,&quot;&quot;), outboundLink.href);
}
</pre>
<h3>The complete code</h3>
<p>Using Prototype version 1.6 the final code might look like this:</p>
<p><strong>Update</strong>: As pointed out in the comments, hard coding a domain into your code isn’t the best idea, <code>window.location.hostname</code> is a good alternative. This may not always work if you do not want to exclude subdomains.</p>
<pre class="prettyprint">
var domainName = &quot;domainname.com&quot;;
// Select all outbound links
$$('a[href^=&quot;http&quot;]:not(a[href*=&quot;'+domainName+'&quot;])').each(function(outboundLink) {
        // Add listener to each of the links
        Event.observe(outboundLink, 'click', function() {
        // category, action, label
        pageTracker._trackEvent('Outbound Link', outboundLink.innerHTML.replace(/(&lt;([^&gt;]+)&gt;)/ig,&quot;&quot;), outboundLink.href);
        }
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.paulrhayes.com/2009-03/track-outbound-links-using-google-analytics/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

