<?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>Jabbering Giraffe &#187; javascript</title>
	<atom:link href="http://happygiraffe.net/blog/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://happygiraffe.net/blog</link>
	<description></description>
	<lastBuildDate>Wed, 19 Oct 2011 10:40:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>No escape() from JavaScript</title>
		<link>http://happygiraffe.net/blog/2009/09/14/no-escape-from-javascript/</link>
		<comments>http://happygiraffe.net/blog/2009/09/14/no-escape-from-javascript/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 12:50:58 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1618</guid>
		<description><![CDATA[A couple of days ago, we got caught out by a few encoding issues in a site at $WORK. The Perl related ones were fairly self explanatory and I&#8217;d seen before (e.g. not calling decode_utf8() on the query string parameters). &#8230; <a href="http://happygiraffe.net/blog/2009/09/14/no-escape-from-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago, we got caught out by a few encoding issues in a site at <code>$WORK</code>.  The Perl related ones were fairly self explanatory and I&#8217;d seen before (e.g. not calling <a href="http://search.cpan.org/dist/Encode/Encode.pm#$string_=_decode_utf8%28$octets_[,_CHECK]%29;"><code>decode_utf8()</code></a> on the query string parameters).  But the JavaScript part was new to me.</p>
<p>The problem was that we were using JavaScript to create an URL, but this wasn&#8217;t encoding some characters correctly.  After a bit of investigation, the problem comes down to the difference between <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#escape_and_unescape_Functions"><code>escape()</code></a> and <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Functions/encodeURIComponent"><code>encodeURIComponent()</code></a>.</p>
<style type="text/css">
#escaper {
  border: 1px dotted black;
  text-align: center;
}
#escaper thead {
  background: #eee;
}
#escaper thead th {
  border-bottom: 1px solid black;
}
#escaper td, #escaper th {
  padding: .25em;
}
</style>
<table id="escaper" cellspacing="0">
<thead>
<tr>
<th>input</th>
<th><code>escape(…)</code></th>
<th><code>encodeURIComponent(…)</code></th>
</tr>
</thead>
<tr>
<td><code>a&#038;b</code></td>
<td><code>a%26b</code></td>
<td><code>a%26b</code></td>
</tr>
<tr>
<td><code>1+2</code></td>
<td><code>1+2</code></td>
<td><code>1%2B2</code></td>
</tr>
<tr>
<td><code>caf&#xE9;</code></td>
<td><code>caf%E9</code></td>
<td><code>caf%C3%A9</code></td>
</tr>
<tr>
<td><code>&#x100;dam</code></td>
<td><code>%u0100dam</code></td>
<td><code>%C4%80dam</code></td>
</tr>
</table>
<p>The last is particularly troublesome, as no server I know of will support decoding that <code>%u</code> form.</p>
<p>The takeaway is that <code>encodeURIComponent()</code> <em>always</em> encodes as UTF-8 and doesn&#8217;t miss characters out.  As far as I can see, this means you should simply never use <code>escape()</code>.  Which is why I&#8217;ve asked Douglas Crockford to <a href="http://tech.groups.yahoo.com/group/jslint_com/message/906">add it as a warning</a> to <a href="http://jslint.com/">JSLint</a>.</p>
<p>Once we switched the site&#8217;s JavaScript from <code>escape()</code> to <code>encodeURIComponent()</code>, everything worked as expected.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2009/09/14/no-escape-from-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Which whitespace was that again?</title>
		<link>http://happygiraffe.net/blog/2009/09/14/which-whitespace-was-that-again/</link>
		<comments>http://happygiraffe.net/blog/2009/09/14/which-whitespace-was-that-again/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 11:55:09 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[msie]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1616</guid>
		<description><![CDATA[We recently saw this at $WORK. It appears corrupted in Internet Explorer only. Firefox and Safari show it normally. After much exploration in the debugger, we eventually found it was caused by using the innerText property in internet explorer. This &#8230; <a href="http://happygiraffe.net/blog/2009/09/14/which-whitespace-was-that-again/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We recently saw this at <code>$WORK</code>.  It appears corrupted in Internet Explorer only.  Firefox and Safari show it normally.</p>
<p><img src="http://happygiraffe.net/blog/wp-content/uploads/2009/09/msie-corrupt-text.png" alt="Corrupted text in internet explorer" border="0" width="664" height="258" /></p>
<p>After much exploration in the debugger, we eventually found it was caused by using the <a href="http://msdn.microsoft.com/en-us/library/ms533899%28VS.85%29.aspx"><code>innerText</code></a> property in internet explorer.  This has the mildly surprising property of turning multiple spaces into U+00A0 (NO-BREAK SPACE) characters (<code>&amp;nbsp;</code> to you and me).  This behaviour doesn&#8217;t appear to be documented.  And before you ask, this was all being done by a <a href="http://chibaxforms.org/">third-party</a> — I know to not use proprietary extensions where possible.</p>
<p>Anyway, I nailed it down to a small test.  Given this markup.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;foo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then this script demonstrates the problem.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// OK</span>
foo.<span style="color: #660066;">innerText</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'A'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">' '</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'B'</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">innerHTML</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// &quot;A B&quot;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// FAIL</span>
foo.<span style="color: #660066;">innerText</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'A'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">' '</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">' '</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'B'</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">innerHTML</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// &quot;A&amp;nbsp; B&quot;</span></pre></div></div>

<p>If you want to try it yourself, check out <a href="http://jsbin.com/anigo">http://jsbin.com/anigo</a>.  (<a href="http://jsbin.com/">jsbin</a> is <em>awesome!</em>  Thanks, <a href="http://remysharp.com/">rem</a>!)</p>
<p>The quick solution is simple: normalize whitespace before insertion before using innerText.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #3366CC;">'some       where        over      the      rainbow'</span><span style="color: #339933;">;</span>
&nbsp;
foo.<span style="color: #660066;">innerText</span> <span style="color: #339933;">=</span> text.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s+/</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Of course, you should really be using <a href="https://developer.mozilla.org/En/DOM/Node.appendChild"><code>appendChild()</code></a> and <a href="https://developer.mozilla.org/en/DOM/document.createTextNode"><code>createTextNode()</code></a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2009/09/14/which-whitespace-was-that-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript.pm on OSX</title>
		<link>http://happygiraffe.net/blog/2009/08/24/javascript-pm-on-osx/</link>
		<comments>http://happygiraffe.net/blog/2009/08/24/javascript-pm-on-osx/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 23:02:29 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1594</guid>
		<description><![CDATA[Just a quick note… I was looking at RT#48699 when I noticed that MacPorts didn&#8217;t have JavaScript.pm in it&#8217;s collection. I needed to install it by hand. Unfortunately, the latest version (1.12) doesn&#8217;t install cleanly. So I&#8217;ve forked it and &#8230; <a href="http://happygiraffe.net/blog/2009/08/24/javascript-pm-on-osx/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Just a quick note…  I was looking at <a href="http://rt.cpan.org/Ticket/Display.html?id=48699">RT#48699</a> when I noticed that <a href="http://www.macports.org/">MacPorts</a> didn&#8217;t have <a href="http://search.cpan.org/perldoc?JavaScript">JavaScript.pm</a> in it&#8217;s collection.  I needed to install it by hand.  Unfortunately, the latest version (1.12) doesn&#8217;t install cleanly.</p>
<p>So I&#8217;ve <a href="http://github.com/happygiraffe/javascript">forked it</a> and <a href="http://github.com/happygiraffe/javascript/commit/e45e5307286697b27a88027d4f1f66e7e6d64c7f">fixed it</a> (along with a couple of other minor nits).</p>
<p>Claes <a href="http://twitter.com/claesjac/status/3305316805">said</a> he&#8217;ll apply the patch at some point.  So hopefully when 1.13 comes out, this won&#8217;t be necessary.</p>
<p>Of course, really I should get to grips with MacPorts and submit a <a href="http://guide.macports.org/#development">Portfile</a>…</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2009/08/24/javascript-pm-on-osx/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jslint.com mirror</title>
		<link>http://happygiraffe.net/blog/2009/08/02/jslint-com-mirror/</link>
		<comments>http://happygiraffe.net/blog/2009/08/02/jslint-com-mirror/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 21:40:10 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jslint4java]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1583</guid>
		<description><![CDATA[Whilst I ensure I have the latest version of JSLint for each release of jslint4java, it&#8217;s often difficult to know what&#8217;s actually changed between versions. Unfortunately, Douglas Crockford doesn&#8217;t maintain a public version control system1 (which is entirely up to &#8230; <a href="http://happygiraffe.net/blog/2009/08/02/jslint-com-mirror/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Whilst I ensure I have the latest version of <a href="http://www.jslint.com/">JSLint</a> for each release of jslint4java, it&#8217;s often difficult to know what&#8217;s actually changed between versions.  Unfortunately, Douglas Crockford doesn&#8217;t maintain a public version control system<sup><a href="#fn1">1</a></sup> (which is entirely up to him).</p>
<p>Nonetheless, it&#8217;s kind of useful to be able to look at a version of JSLint and say &#8220;this changed since the last version&#8221;.  So, I&#8217;ve started mirroring <a href="http://jslint.com/"><code>www.jslint.com</code></a> into a <a href="http://github.com/happygiraffe/jslint.com-mirror/tree">git repository</a>.  I check for updates every hour.</p>
<p>This means you can now see how JSLint changes over time.</p>
<p>Now, it&#8217;s not as useful as it could be.  It would be nice to tie these up to the changes that Douglas posts to <a href="http://tech.dir.groups.yahoo.com/group/jslint_com/messages/">the mailing list</a> (e.g. <a href="http://tech.dir.groups.yahoo.com/group/jslint_com/message/764">announcing the new <code>.data()</code> support</a>).  But that&#8217;s quite a bit more work.</p>
<p>I hope that this is beneficial to somebody.</p>
<p><sup><a name="fn1">1</a></sup> It&#8217;s amazing how normal public version control has become over the years.  This used to be a <em>lot</em> less common, which made projects a lot harder to understand.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2009/08/02/jslint-com-mirror/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Analytics in XHTML</title>
		<link>http://happygiraffe.net/blog/2009/06/06/google-analytics-in-xhtml/</link>
		<comments>http://happygiraffe.net/blog/2009/06/06/google-analytics-in-xhtml/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 18:58:39 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1530</guid>
		<description><![CDATA[I&#8217;e been attempting to get Google Analytics to work correctly in both FireFox and IE6 for a site at $WORK. This is not normally a problem, apart from the fact that we&#8217;re serving up pages to firefox as application/xhtml+xml in &#8230; <a href="http://happygiraffe.net/blog/2009/06/06/google-analytics-in-xhtml/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;e been attempting to get Google Analytics to work correctly in both FireFox and IE6 for a site at $WORK.  This is not normally a problem, apart from the fact that we&#8217;re serving up pages to firefox as <code>application/xhtml+xml</code> in order to get <a href="http://en.wikipedia.org/wiki/MathML">MathML</a> support.</p>
<p>Now, the sample code from Google is pretty gnarly.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
var gaJsHost = ((&quot;https:&quot; == document.location.protocol) ? &quot;https://ssl.&quot; : &quot;http://www.&quot;);
document.write(unescape(&quot;%3Cscript src='&quot; + gaJsHost + &quot;google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E&quot;));
<span style="color: #009900;">&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>
try{
var pageTracker = _gat._getTracker(&quot;UA-xxxxxx-x&quot;);
pageTracker._trackPageview();
} catch(err) {}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>This fails in XHTML as <a href="http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite">document.write() isn&#8217;t there</a>.</p>
<p>I tried a number of ways to get this to work.</p>
<ul>
<li>Replace <code>document.write()</code> with some jQuery code to insert a script tag.
<ul>
<li>This didn&#8217;t work in IE6 &#8212; as the second script block ended up getting called before newly inserted script tag had loaded.</li>
<li> But I did find out that jQuery will replace script tags with Ajax calls for you.  Which means you don&#8217;t end up with a script tag in the DOM tree, which is highly confusing when you&#8217;re looking for it in firebug.</li>
</ul>
</li>
<li>Replace <code>document.write()</code> with native DOM calls to insert a script tag.
<ul>
<li>I did find the neat idea of adding an id to the script tag you&#8217;re currently in, so you know where to insert new DOM elements.</li>
<li>But it still failed, and for the same reason as above.</li>
</ul>
</li>
</ul>
<p>I was just about to start implementing something <em>evil</em> involving <code>setInterval()</code>, when I realised…</p>
<p>… this site will never use SSL!</p>
<p>So I replaced the code to generate a script tag <em>with the script tag</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">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;">'http://www.google-analytics.com/ga.js'</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>
try{
var pageTracker = _gat._getTracker(&quot;UA-xxxxxx-x&quot;);
pageTracker._trackPageview();
} catch(err) {}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>Tada!  If only I&#8217;d thought of this a few hours earlier…  The moral is to be more aware of the context in which you&#8217;re doing something.  Keep an eye on the &#8220;big picture&#8221; to use a particularly horrible metaphor.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2009/06/06/google-analytics-in-xhtml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Time Zones, done better</title>
		<link>http://happygiraffe.net/blog/2008/11/09/time-zones-done-better/</link>
		<comments>http://happygiraffe.net/blog/2008/11/09/time-zones-done-better/#comments</comments>
		<pubDate>Sun, 09 Nov 2008 21:48:30 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1409</guid>
		<description><![CDATA[A few days ago, I signed up to HuffDuffer (love the logo, BTW). However, once I&#8217;d registered, my profile page said Huffduffing since November 1st, 2008. Which was a little weird, as when I did this it was October 31st. &#8230; <a href="http://happygiraffe.net/blog/2008/11/09/time-zones-done-better/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A few days ago, I signed up to <a href="http://huffduffer.com/">HuffDuffer</a> (<em>love</em> the logo, BTW).  However, once I&#8217;d registered, my profile page said <q>Huffduffing since November 1st, 2008</q>.  Which was a little weird, as when I did this it was October 31<sup>st</sup>.</p>
<p>So I filed a bug report and <a href="http://adactio.com/">Jeremy</a> quickly spotted the problem: the server was running in Australia, so was using an <a href="http://en.wikipedia.org/wiki/Time_in_Australia">Australian timezone</a>.  That&#8217;s about 11 hours ahead, which would put my signup in to the next day.  His fix was simple: force huffduffer to use <a href="http://en.wikipedia.org/wiki/Greenwich_Mean_Time">GMT</a> (see <a href="http://php.net/manual/en/datetime.configuration.php">datetime configuration</a> in the PHP manual).  This isn&#8217;t a 100% correct solution, but it&#8217;s definitely the least bad approach.</p>
<p>But it set me wondering: can we do better?  The key thing is that something somewhere has to know what time zone you&#8217;re in.  That thing happens to be your browser.  JavaScript <code>Date</code> objects have a <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/getTimezoneOffset"><code>getTimezoneOffset()</code></a> method which return the minutes difference between you and GMT.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTimezoneOffset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Right now, when I <a href="javascript:alert(new Date().getTimezoneOffset());">try it</a>, it returns zero because I&#8217;m in british winter time, which just happens to be the same as GMT.  If I was in Australia, it&#8217;d return 660 (11 hours).</p>
<p>So we know the time on the server, and we know how far away from GMT we are.  So how do we get JavaScript to format the date correctly?</p>
<p>Obviously, the first step is to pass the time to the browser as GMT.  The normal approach to this is to use the <a href="http://en.wikipedia.org/wiki/Unix_time">Unix time</a> format (number of seconds since midnight 1970).  In PHP, look at <a href="http://www.php.net/manual/en/function.gmmktime.php">gmmktime()</a>.  Perl uses <a href="http://perldoc.perl.org/functions/gmtime.html">gmtime()</a>.  And JavaScript has a constructor which takes milliseconds.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Thu Jan 01 1970 01:00:00 GMT+0100 (BST)</span></pre></div></div>

<p>Ooops.  That&#8217;s an hour ahead.  But it&#8217;s actually misleading: the internal representation (the milliseconds we are passing in) doesn&#8217;t have a timezone, it&#8217;s only applied when you turn it into a human readable form.  You can demonstrate this by using <a href="http://www.mozilla.org/rhino/">rhino</a> on the command line to fool Javascript in to thinking it&#8217;s in different time zones<sup><a href="#fn1">1</a></sup>.</p>
<p>First, let&#8217;s try it in my time zone:</p>
<pre>
  % java -jar js.jar -e "print(new Date(0))"
  Thu Jan 01 1970 00:00:00 GMT-0000 (GMT)
</pre>
<p>Now let&#8217;s pretend we&#8217;re in Australia:</p>
<pre>
  % TZ=Australia/Melbourne java -jar js.jar -e 'print(new Date(0))'
  Thu Jan 01 1970 10:00:00 GMT+1000 (EST)
</pre>
<p>Which makes things really easy for us: get the server to pass the unix time, create a new Date object from it and then rewrite the textual object based on the contents.</p>
<p>So, if you start out with some HTML like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">  &lt;p&gt;At the third stroke, it will be &lt;span class=&quot;datetime&quot; unixtime=&quot;1225461600&quot;&gt;2008-10-31 14:00:00&lt;/span&gt;.&lt;/p&gt;</pre></div></div>

<p>Note that we include the default (GMT) time in order to degrade gracefully.</p>
<p>Then you can use some JavaScript like this to automatically adjust it to the time zone of the browser.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;span.datetime&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><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><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">hasAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'unixtime'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">var</span> unixtime <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'unixtime'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// Convert seconds to milliseconds</span>
        <span style="color: #003366; font-weight: bold;">var</span> date <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>unixtime <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: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>format_date<span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;m relying on <a href="http://jquery.com/">jquery</a> a little bit in there in order to focus on the problem, not the DOM.  You can download the full source if needed (<a href="http://happygiraffe.net/blog/wp-content/uploads/2008/11/tzadjust.zip" title="tzadjust.zip">tzadjust.zip</a>).</p>
<p>This is such a teeny-tiny thing overall, but it&#8217;s part of the polish to help make your site great.</p>
<p><sup id="fn1">1</sup> You can change the timezone of any command this way in Unix.  I have <code>alias nydate='TZ=America/New_York date'</code> in my shell profile so I can quickly see what the time in New York is.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2008/11/09/time-zones-done-better/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jslint4java 1.2</title>
		<link>http://happygiraffe.net/blog/2008/09/07/jslint4java-12/</link>
		<comments>http://happygiraffe.net/blog/2008/09/07/jslint4java-12/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 00:07:25 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jslint4java]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1369</guid>
		<description><![CDATA[I&#8217;ve finally gotten around to finishing off the code that I&#8217;ve had sitting around in google code for over a year, and released jslint4java 1.2. The changes are actually pretty small: Update to jslint 2008-09-04. This adds several new options. &#8230; <a href="http://happygiraffe.net/blog/2008/09/07/jslint4java-12/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve <em>finally</em> gotten around to finishing off the code that I&#8217;ve had sitting around in google code for over a year, and released <a href="http://code.google.com/p/jslint4java/">jslint4java 1.2</a>.  The changes are actually pretty small:</p>
<ul>
<li>Update to <a href="http://www.jslint.com/">jslint</a> 2008-09-04.  This adds several new options.</li>
<li>Several updates to the ant task:
<ul>
<li>Move the antlib definition to &#8220;antlib:net.happygiraffe.jslint&#8221; <strong>(incompatible change)</strong>.</li>
<li>Default to failing the build if validation fails <strong>(incompatible change)</strong>.</li>
<li>Allow use of the <a href="http://ant.apache.org/manual/CoreTypes/fileset.html">fileset</a> element to be more flexible about specifying where your javascript files are. This replaces several attributes on the jslint task <strong>(incompatible change)</strong>.</li>
<li>Add a formatter sub-element, which can output either XML or plain text, either to the console or to a file.</li>
<li>See the <a href="http://code.google.com/p/jslint4java/wiki/JSLintAntTask">documentation</a> for some examples of the new usage.</li>
</ul>
</li>
<li>Allow access to the HTML report produced by JSLint through an API.</li>
</ul>
<p>I&#8217;ve also uploaded the <a href="http://jslint4java.googlecode.com/svn/apidoc/1.2/index.html">javadoc</a> directly into subversion on google code, so it&#8217;s permanently online.  The google-collections guys seem to do this, so it can&#8217;t be a bad idea, right?</p>
<p>Typical.  Five minutes after I release, I notice that it&#8217;s been compiled with 1.6 instead of 1.5.  So, I&#8217;ve just released v1.2.1…</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2008/09/07/jslint4java-12/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Scope</title>
		<link>http://happygiraffe.net/blog/2008/08/27/javascript-scope/</link>
		<comments>http://happygiraffe.net/blog/2008/08/27/javascript-scope/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 09:06:15 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1333</guid>
		<description><![CDATA[This is an issue that popped up with a colleague yesterday. Roughly, there was some code like this. function setUpStuff&#40;&#41; &#123; var items = cssQuery&#40;&#34;#stuff p&#34;&#41;; for &#40;var i = 0; i &#60; items.length; i++&#41; &#123; var item = items&#91;i&#93;; &#8230; <a href="http://happygiraffe.net/blog/2008/08/27/javascript-scope/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is an issue that popped up with a colleague yesterday.  Roughly, there was some code like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> setUpStuff<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> items <span style="color: #339933;">=</span> cssQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#stuff p&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> items.<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>
      <span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">=</span> items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">item</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>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;item is &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</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: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Unfortunately, even if <code>cssQuery()</code> returns a list of three items, every call to <code>alert()</code> will show the last item.  Why?</p>
<p>It&#8217;s all down to the fact that JavaScript doesn&#8217;t have block-scoped variables.  It only has function-scope.</p>
<p>In the context of the code above, it means that <em>item</em> exists from the point of entry to <code>setUpStuff()</code>.  It&#8217;s just <a href="http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Properties/Undefined">undefined</a> until the we get into the loop.  The consequence of this is that each time we assign <code>onclick</code>, we&#8217;re referring to the <em>same variable</em>.</p>
<p>The solution is to create another function scope, of course.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> setUpStuff<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> items <span style="color: #339933;">=</span> cssQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#stuff p&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> items.<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>
      setUpAStuff<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  functon setUpAStuff<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">item</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>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;item is &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</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: #009900;">&#125;</span></pre></div></div>

<p>This now works as expected, because inside <code>setUpAStuff()</code>, we&#8217;re referring to a different variable each time (you could also use an anonymous function).  This is definitely one of the more confusing areas of JavaScript (though <a href="http://jslint.com/" title="WARNING: JSLint may hurt your feelings.">JSLint</a> does pick up on this).</p>
<p>* <a href="http://weblog.raganwald.com/2007/08/block-structured-javascript.html">Block-Structured JavaScript</a><br />
* <a href="http://developer.mozilla.org/En/Core_JavaScript_1.5_Guide:Block_Statement">Core JavaScript 1.5 Guide:Block Statement</a> (on devmo)</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2008/08/27/javascript-scope/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Argumentative</title>
		<link>http://happygiraffe.net/blog/2008/01/25/argumentative/</link>
		<comments>http://happygiraffe.net/blog/2008/01/25/argumentative/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 07:29:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2008/01/25/argumentative/</guid>
		<description><![CDATA[I spent a little while looking at mootools yesterday for a colleague. It&#8217;s yet another JavaScript library. My colleague was wondering how to restrict the Accordion effect so it applied once to each area of content on the page, rather &#8230; <a href="http://happygiraffe.net/blog/2008/01/25/argumentative/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I spent a little while looking at <a href="http://mootools.net/">mootools</a> yesterday for a colleague.  It&#8217;s yet another JavaScript library.  My colleague was wondering how to restrict the <a href="http://docs.mootools.net/Plugins/Accordion.js">Accordion</a> effect so it applied once to each area of content on the page, rather than once for the whole page (each content area has multiple bits of content where only one should be visible).</p>
<p>As usual, I just headed straight for the source code (<a href="http://dev.mootools.net/browser/tags/1-11/Plugins/Accordion.js">Accordion.js</a>).  Inside, I found the best abuse of JavaScript I&#8217;ve seen in a while.</p>
<pre>
  initialize: function(){
    var options, togglers, elements, container;
    $each(arguments, function(argument, i){
            switch($type(argument)){
                    case 'object': options = argument; break;
                    case 'element': container = $(argument); break;
                    default:
                            var temp = $$(argument);
                            if (!togglers) togglers = temp;
                            else elements = temp;
            }
    });
    // …
  }
</pre>
<p>Now what exactly does this do?  It&#8217;s pretty different from the usual JavaScript function usage:</p>
<pre>
  var initialize = function (options, togglers, elements, container) { … }
</pre>
<p>The first thing of interest is the first parameter to <code>$each</code>: <a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions:arguments">arguments</a>.  A little known feature of JavaScript is that every function has an array-like object of all its arguments available.  You can find get a reference to the function that you&#8217;re in, which is sometimes useful.</p>
<p>Here, it&#8217;s being used to accept an arbitrary number of arguments, <em>in any order</em>.  To be quite frank, it&#8217;s a bit of a mess.  This is my understanding of the above code:</p>
<ul>
<li>You can pass in up to four arguments.
<ul>
<li>You can pass in more, but we only get 4 parameters out of it.</li>
</ul>
</li>
<li>The last JavaScript object will get treated as a set of options.</li>
<li>The last <em>element</em> passed in will be used as &#8220;container&#8221;.</li>
<li>The first non object|element will be passed to <a href="http://docs.mootools.net/Native/Element.js#$$">$$</a> and treated as the set of &#8220;togglers&#8221;.</li>
<li>The last non object|element arguments will be passed through $$ and treated as the elements to be shown/hidden.</li>
</ul>
<p>Is that right?  I&#8217;ve been looking at it for a bit and still not 100% sure.</p>
<p>To me, this is a fine example of taking the flexibility of JavaScript just a little <em>too</em> far.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2008/01/25/argumentative/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Clich&#233;s are hard</title>
		<link>http://happygiraffe.net/blog/2007/12/19/cliches-are-hard/</link>
		<comments>http://happygiraffe.net/blog/2007/12/19/cliches-are-hard/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 06:42:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2007/12/19/cliches-are-hard/</guid>
		<description><![CDATA[So yesterday, Jeremy asked: Wondering if accents are valid in class names (so I can mark up some text as being of the class &#8220;cliché&#8221;) It&#8217;s a damned good question. And you have to consider: character encodings; CSS; HTML; XHTML; &#8230; <a href="http://happygiraffe.net/blog/2007/12/19/cliches-are-hard/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So yesterday, Jeremy <a href="http://twitter.com/adactio/statuses/511587712">asked</a>:</p>
<blockquote>
<p>Wondering if accents are valid in class names (so I can mark up some text as being of the class &#8220;cliché&#8221;)</p>
</blockquote>
<p>It&#8217;s a damned good question.  And you have to consider: character encodings; <span class="caps">CSS</span>; HTML; <span class="caps">XHTML</span>; JavaScript; <span class="caps">HTTP</span>.  Needless to say, it&#8217;s more complicated than it looks at first.</p>
<p>My first thought was that of <span class="caps">CSS</span> files.  Is it valid to say:</p>
<pre class="css">
  p.cliché { color: #f00; }
</pre>
<p>To answer this question, you have to visit the <a href="http://www.w3.org/TR/CSS21/"><span class="caps">CSS 2</span>.1 spec</a>.  Near the end is <a href="http://www.w3.org/TR/CSS21/grammar.html#grammar">§G.1 Grammar</a>.  It contains a <a href="http://en.wikipedia.org/wiki/Backus–Naur_form"><span class="caps">BNF</span></a> grammar describing the syntax of <span class="caps">CSS</span>.  They&#8217;re not that difficult to read when you get the hang of them.  In this case, I start by finding something I can recognise: a selector.  Then, I work down through the grammar to find what I&#8217;m interested in.</p>
<ul>
<li><code>selector : simple_selector [ combinator simple_selector ]* ;</code>
<ul>
<li>a selector is composed of one or more simple_selectors.</li>
</ul>
</li>
<li><code>simple_selector : element_name [ HASH | class | attrib | pseudo ]* | [ HASH | class | attrib | pseudo ]+ ;</code>
<ul>
<li>A simple_selector is composed of an element_name followed by zero or more an ID, class, attribute or pseudo selector.  Alternatively, it&#8217;s composed of one or more ID/class/attribute/pseudo selector (without an element name).</li>
</ul>
</li>
<li><code>class : '.' IDENT ;</code>
<ul>
<li>A class name is just &#8221;.&#8221; followed by an identifier.  That&#8217;s what we&#8217;re interested in here.</li>
</ul>
</li>
<li><code>ident    -?{nmstart}{nmchar}*</code>
<ul>
<li>This is now in §G.2.  But you can see that an identifier has an optional leading minus, followed by an nmstart and zero or more nmchar.  It&#8217;s those nmchar that we care about.</li>
</ul>
</li>
<li><code>nmchar [_a-z0-9-]|{nonascii}|{escape}</code>
<ul>
<li>nmchar allows letters, numbers, underscores and minuses, as well as non-ascii characters and escapes.  Oooh!  Getting closer!</li>
</ul>
</li>
<li><code>nonascii    [\200-\377]</code>
<ul>
<li>This is a horrid notation.  It&#8217;s an <a href="http://en.wikipedia.org/wiki/Octal">octal</a> character range.  Octal stopped being in general usage in the early 80s, although Unix and C perpetuate them.  Anyway, it says that any character whose code is between 128 and 255 is allowed.</li>
</ul>
</li>
</ul>
<p>So we get an answer: Because é is U+00E9 (or, 233 decimal), it&#8217;s allowed as part of an identifier in a <span class="caps">CSS</span> file.</p>
<p>But it&#8217;s worth noting the arbitrary limit of 255 here.  That means that you don&#8217;t get to use any unicode character above that (e.g. Ā [U+0100]) verbatim in a <span class="caps">CSS</span> file.  Instead, you have to escape it by saying (according to the <code>escape</code> declaration in that grammar) <code>\h100</code>.  Which is quite nasty.</p>
<p>There&#8217;s one other wrinkle to consider before this will work.  You also need to ensure that the <span class="caps">CSS</span> file is served over <span class="caps">HTTP</span> using the correct character set.  If you&#8217;ve saved it as <a href="http://en.wikipedia.org/wiki/ISO/IEC_8859-1">Latin-1</a>, you need to ensure that it&#8217;s served up with this header:</p>
<pre>
  Content-Type: text/css; charset="iso-8859-1"
</pre>
<p>This is the default, so it could be left off, but it&#8217;s usually better to be explicit.  Likewise, if the file is saved as <a href="http://en.wikipedia.org/wiki/UTF-8"><span class="caps">UTF</span>-8</a>, you need this header to be added.</p>
<pre>
  Content-Type: text/css; charset="UTF-8"
</pre>
<p>If you&#8217;re using <a href="http://httpd.apache.org/">Apache</a>, check out the <a href="http://httpd.apache.org/docs/2.2/mod/core.html#adddefaultcharset">AddDefaultCharset</a> and <a href="http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addcharset">AddCharset</a> directives.</p>
<p>So that&#8217;s <span class="caps">CSS</span>.  But what about <span class="caps">HTML</span>?</p>
<p><span class="caps">HTML</span> is defined in the <a href="http://www.w3.org/TR/REC-html40/"><span class="caps">HTML 4</span>.01 specification</a>.  It&#8217;s defined using <a href="http://en.wikipedia.org/wiki/Standard_Generalized_Markup_Language"><span class="caps">SGML</span></a>, which means more complication in order to work out what the heck&#8217;s going on.  Thankfully, everybody knows that there are four ways to get an é into <span class="caps">HTML</span>:</p>
<ul>
<li>A literal <code>é</code>.</li>
<li>A character entity: <code>&amp;eacute;</code></li>
<li>A decimal character reference: <code>&amp;233;</code></li>
<li>A hex character reference: <code>&amp;xE9;</code></li>
</ul>
<p>In order to figure out what characters are allowed in a class attribute, though, you have to go and start looking at the <a href="http://www.w3.org/TR/REC-html40/sgml/dtd.html"><span class="caps">DTD</span></a>:</p>
<ul>
<li>The <a href="http://www.w3.org/TR/REC-html40/sgml/dtd.html#coreattrs">coreattrs</a> entity is the first mention.  It defines a class as being some <span class="caps">CDATA</span>.</li>
<li>The definition of <span class="caps">CDATA</span> is an intrinsic part of <span class="caps">SGML</span>.  The details of which can be altered by the <a href="http://www.w3.org/TR/REC-html40/sgml/sgmldecl.html"><span class="caps">SGML</span> Declaration for <span class="caps">HTML 4</span></a>.  There&#8217;s a section at the beginning which lists which characters are allowed.  It includes a large number of unicode characters all above 160 decimal.</li>
</ul>
<p>That means that it&#8217;s safe to include a character via any of the above methods.</p>
<p>But there are a few more wrinkles.  Firstly, whilst the two characters references above are intrinsic to <span class="caps">HTML</span> (via <span class="caps">SGML</span>), where does the character entity come from?  Well, they are defined as part of the <span class="caps">HTML</span> spec: <a href="http://www.w3.org/TR/REC-html40/sgml/entities.html">Character entity references in <span class="caps">HTML 4</span></a>.</p>
<p>There&#8217;s also the problem of the character encoding in case you use the literal é.  Like the <span class="caps">CSS</span> above, you need to ensure that your web server is telling everybody what character encoding the file is served as.  Actually, for <span class="caps">HTML</span>, it&#8217;s less of a problem, as the browser will generally auto-detect character encodings.  But that&#8217;s not necessarily reliable, so it&#8217;s better to be explicit.  And in <span class="caps">HTML</span>, you can put the character encoding in the file itself:</p>
<pre>
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
</pre>
<p>Yes, this a little bit like having a french dictionary containing the words &#8220;ecrit en Français&#8221; in the front.   But it&#8217;s a good idea to have both this and the <span class="caps">HTTP</span> declaration (and they <em>must</em> match).</p>
<p>I&#8217;m not going to talk about <span class="caps">XHTML</span>/XML because it&#8217;s not in widespread use (i.e. serving it up as <code>application/xhtml+xml</code>).</p>
<p>Finally, what about JavaScript?  Well, it&#8217;s defined as <a href="http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf"><span class="caps">ECMA</span>-262</a> (3rd edition).  That spec explicitly defines everything in terms of Unicode, so it&#8217;s mostly OK.  You can still access characters you can&#8217;t type via an escape mechanism: <code>\u00E9</code> (see the definition of UnicodeEscapeSequence on page 19).  Additionally, JavaScript can get at the unicode characters in the <span class="caps">DOM</span> quite easily:</p>
<pre>
  &lt;p id='a1' class="cliché"&gt;Lessons will be learned.&lt;/p&gt;
  &lt;script type="text/javascript"&gt;
    alert(document.getElementById('a1').className)
  &lt;/script&gt;
</pre>
<p>As always, JavaScript files served over <span class="caps">HTTP</span> need to be supplied with the correct character-encoding through the Content-Type header.  Just like <span class="caps">CSS</span> and <span class="caps">HTML</span>.</p>
<p>So what&#8217;s the take-away from all this?</p>
<ul>
<li>Use literal characters and <span class="caps">UTF</span>-8 everywhere.  It&#8217;s consistent and extensible.</li>
<li>Know how to look in the specs when something&#8217;s going wrong – you&#8217;ll know whether it&#8217;s you, or the browser that&#8217;s getting it wrong.</li>
<li>Characters are hard, let&#8217;s go shopping!</li>
</ul>
<p>Jeremy <a href="http://twitter.com/adactio/statuses/511627392">worked it all out</a> in far less time than I did.</p>
<blockquote>
<p>Figuring I should be okay as long as I use a character entity. <a href="http://tinyurl.com/7p7qc">http://tinyurl.com/7p7qc</a></p>
</blockquote>
<p>Looking at that link, I notice that <span class="caps">CDATA</span> is handled specially within <span class="caps">STYLE</span> and <span class="caps">SCRIPT</span> tags.  Yet more exceptions to the rules!</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2007/12/19/cliches-are-hard/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

