<?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>Barklund.org &#187; JavaScript</title>
	<atom:link href="http://www.barklund.org/blog/category/programming/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barklund.org/blog</link>
	<description>work smarter when building current web trends</description>
	<lastBuildDate>Wed, 26 May 2010 09:49:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>iPad detection the right way</title>
		<link>http://www.barklund.org/blog/2010/04/23/ipad-detection/</link>
		<comments>http://www.barklund.org/blog/2010/04/23/ipad-detection/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 09:22:34 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Konstellation]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Trends]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=760</guid>
		<description><![CDATA[Here at Konstellation, we just launched the PageGangster for mobile devices including iPad, iPhone, iPod touch and Android. I developed the clientside for this in JavaScript and we launched it with automatic user agent detection serverside. But this had some problems. I read some info about this and conclude, that some iPad user agent strings [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/11/23/iphone-developer-boycott-in-the-works/' rel='bookmark' title='Permanent Link: The iPhone developer boycott in the works'>The iPhone developer boycott in the works</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/11/coordinate-proxy-january-2010-idea/' rel='bookmark' title='Permanent Link: Coordinate-Proxy &#8211; January 2010 Ideas'>Coordinate-Proxy &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/07/app-idea-store-january-2010-idea/' rel='bookmark' title='Permanent Link: App Idea Store &#8211; January 2010 Ideas'>App Idea Store &#8211; January 2010 Ideas</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div id="attachment_766" class="wp-caption alignright" style="width: 160px"><img src="http://www.barklund.org/blog/wp-content/uploads/2010/04/ipad-300x241.jpg" alt="iPad in action" title="iPad" width="150" height="120" class="size-medium wp-image-766" /><p class="wp-caption-text">The famous large flat thing slightly more useful than a rock</p></div>
<p>Here at <a href="http://konstellation.dk/" title="Konstellation homepage">Konstellation</a>, we just launched the <a href="http://pagegangster.com" title="PageGangster - convert your PDF to an experience">PageGangster</a> for <a href="http://www.pagegangster.com/mobile-devices/" title="Introduction about PageGangster for mobile devices">mobile devices</a> including iPad, iPhone, iPod touch and Android. I developed the clientside for this in JavaScript and we launched it with automatic user agent detection serverside. But this had some problems.</p>
<p><span id="more-760"></span></p>
<p>I read <a href="http://www.nczonline.net/blog/2010/04/06/ipad-web-development-tips/" title="iPad web development tips from nczonline.net">some info</a> about this and conclude, that some iPad user agent strings include the word &#8220;iPhone&#8221; and some users of all types of devices fake their user agent strings.</p>
<p>But then I read on the above link, that no matter what, the <code>navigator.platform</code> attribute would always be set to the right type of device. So in the regular web version of the publication viewer we have now (which uses Flash), we have added a JavaScript detecting if the serverside script did not redirect the client properly:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>navigator <span style="color: #339933;">&amp;&amp;</span> navigator.<span style="color: #660066;">platform</span> <span style="color: #339933;">&amp;&amp;</span> navigator.<span style="color: #660066;">platform</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^(iPad|iPod|iPhone)$/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	location.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">href</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;?force-mobile&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It seems simple, but solves the problem of some users seeing the dreaded blue box instead of their publication optimized for their device.</p>
<p>On another node, this experience of developing a web application for the iPad/iPhone was very rewarding and I gained many insights, which might spawn other posts. And if you have any questions about it in any way, feel free to ask here &#8211; but please do not submit bug reports about the PageGangster for mobile &#8211; do that via <a href="mailto:info@pagegangster.com">info@pagegangster.com</a> instead.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/11/23/iphone-developer-boycott-in-the-works/' rel='bookmark' title='Permanent Link: The iPhone developer boycott in the works'>The iPhone developer boycott in the works</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/11/coordinate-proxy-january-2010-idea/' rel='bookmark' title='Permanent Link: Coordinate-Proxy &#8211; January 2010 Ideas'>Coordinate-Proxy &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/07/app-idea-store-january-2010-idea/' rel='bookmark' title='Permanent Link: App Idea Store &#8211; January 2010 Ideas'>App Idea Store &#8211; January 2010 Ideas</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2010/04/23/ipad-detection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Social Traffic Analyzer &#8211; January 2010 Ideas</title>
		<link>http://www.barklund.org/blog/2010/01/22/social-traffic-analyzer-january-2010-idea/</link>
		<comments>http://www.barklund.org/blog/2010/01/22/social-traffic-analyzer-january-2010-idea/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 08:00:12 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[January 2010 Ideas]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Trends]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=637</guid>
		<description><![CDATA[The twenty-second idea for my 365 social ideas is not really a clear-cut idea &#8211; yet. But I feel a need for a new website traffic analyzing service. Google Analytics is definitely the mostly used and best free service. But they have many shortcomings, and I definitely would like to see a new player enter [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/01/04/social-subway-january-2010-idea/' rel='bookmark' title='Permanent Link: Social Subway &#8211; January 2010 Ideas'>Social Subway &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/09/comment-on-everything-january-2010-idea/' rel='bookmark' title='Permanent Link: Comment On Everything &#8211; January 2010 Ideas'>Comment On Everything &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/11/coordinate-proxy-january-2010-idea/' rel='bookmark' title='Permanent Link: Coordinate-Proxy &#8211; January 2010 Ideas'>Coordinate-Proxy &#8211; January 2010 Ideas</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The twenty-second idea for my <a href="/blog/365ideas/" title="365 Social Ideas from Barklund.org">365 social ideas</a> is not really a clear-cut idea &#8211; yet. But I feel a need for a new website traffic analyzing service. <a href="http://www.google.com/analytics/">Google Analytics</a> is definitely the mostly used and best free service. But they have many shortcomings, and I definitely would like to see a new player enter this playing field. And while where add it, there are some new trends and actions, that current has a huge influence on current traffic trends, that you cannot track fully: social media traffic.</p>
<p><span id="more-637"></span></p>
<p>I do not know how to integrate all these things, but a good traffic analyzer should know not only who links to you but also who is sharing links to your services through which channels. It should report which channels generate the most traffic, but also who pushes the links to these channels etc. If someone e.g. tweets/diggs/reddits about your service/website a lot, it should be reflected.</p>
<p>About the short-comings of Google Analytics, my main concern is, that when experimenting with how you can track new things, there is a 1-4 hour delay on page tracks and up to 3 days delay on custom variables tracking (in my experience). A good provider really ought to have a development option available, that only allow limited traffic but provide almost-instant analysis of this data.</p>
<h4 id="toc-why">Why?</h4>
<p>This is one of the simplest of Google&#8217;s services, and thus we really need a contender. With the availability of cheap clouds, it should be quite easy to set up, though it would require some funding to provide it to people for free. Google Analytics does not innovate that much &#8211; not in the social media tracking area at least. They are more interested in developing new things in the &#8220;how can I make people buy more crap from me&#8221; category for web shop owners.</p>
<h4 id="toc-whats-next">What&#8217;s next?</h4>
<p>Do with this idea whatever you like &#8211; expand, implement, trash or forget. Just remember, that if you use it in anyway make sure to attribute me according to the Creative Commons Attribution 3.0 License, that all these <a href="/blog/365ideas" title="365 Social Ideas from Barklund.org">365 Social Ideas</a> are published under.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/01/04/social-subway-january-2010-idea/' rel='bookmark' title='Permanent Link: Social Subway &#8211; January 2010 Ideas'>Social Subway &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/09/comment-on-everything-january-2010-idea/' rel='bookmark' title='Permanent Link: Comment On Everything &#8211; January 2010 Ideas'>Comment On Everything &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/11/coordinate-proxy-january-2010-idea/' rel='bookmark' title='Permanent Link: Coordinate-Proxy &#8211; January 2010 Ideas'>Coordinate-Proxy &#8211; January 2010 Ideas</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2010/01/22/social-traffic-analyzer-january-2010-idea/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copy-Paste Injection &#8211; January 2010 Ideas</title>
		<link>http://www.barklund.org/blog/2010/01/16/copy-paste-injection-january-2010-idea/</link>
		<comments>http://www.barklund.org/blog/2010/01/16/copy-paste-injection-january-2010-idea/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 08:00:50 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[DOM 2 Traversal and Range]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[January 2010 Ideas]]></category>
		<category><![CDATA[Online Rights]]></category>
		<category><![CDATA[Trends]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=614</guid>
		<description><![CDATA[The sixteenth idea for my 365 social ideas is an idea for a technical tool to assist everyone in getting the credit they deserve and the loyalty they can expect: a copy-paste injection script. It is the very same idea that tynt.com has &#8220;created&#8221;, but they keep the technology (how simple it may be) to [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/01/22/social-traffic-analyzer-january-2010-idea/' rel='bookmark' title='Permanent Link: Social Traffic Analyzer &#8211; January 2010 Ideas'>Social Traffic Analyzer &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/09/comment-on-everything-january-2010-idea/' rel='bookmark' title='Permanent Link: Comment On Everything &#8211; January 2010 Ideas'>Comment On Everything &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/07/app-idea-store-january-2010-idea/' rel='bookmark' title='Permanent Link: App Idea Store &#8211; January 2010 Ideas'>App Idea Store &#8211; January 2010 Ideas</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The sixteenth idea for my <a href="/blog/365ideas/" title="365 Social Ideas from Barklund.org">365 social ideas</a> is an idea for a technical tool to assist everyone in getting the credit they deserve and the loyalty they can expect: a copy-paste injection script. It is the very same idea that <a href="http://www.tynt.com/" title="Tynt.com">tynt.com</a> has &#8220;created&#8221;, but they keep the technology (how simple it may be) to themselves and have <a href="http://www1.tynt.com/patents" title="Tynt.com about patents">even filed for a patent</a>.</p>
<p><span id="more-614"></span></p>
<p>Well, I have implemented a <a href="/examples/copypaste">very simple demo</a> of the first steps. So far, <a href="/examples/copypaste/jquery.copypaste.js">the script</a> only makes sure to add some extra text to the current selection when ever the mouse button is released (which is how most users select most text) and it only works in Firefox (tested in 3.6). Next steps include making the link direct to the actually selected content, record text copying in Google Analytics, add cross-browser checks etc.</p>
<p>The script is published under the same license as these ideas, CC-BY 3.0.</p>
<h4 id="toc-why">Why?</h4>
<p>Copy-paste is another quite common way of interacting with websites and allowing people to easily spread the word about your website in a new way ensures you credit where such is due. Creating a full-featured jQuery-plugin from this should be fairly easy and then making sure it integrates with Google Analytics if present &#8211; and then we&#8217;re actually done.</p>
<h4 id="toc-whats-next">What&#8217;s next?</h4>
<p>Do with this idea whatever you like &#8211; expand, implement, trash or forget. Just remember, that if you use it in anyway make sure to attribute me according to the Creative Commons Attribution 3.0 License, that all these <a href="/blog/365ideas" title="365 Social Ideas from Barklund.org">365 Social Ideas</a> are published under.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/01/22/social-traffic-analyzer-january-2010-idea/' rel='bookmark' title='Permanent Link: Social Traffic Analyzer &#8211; January 2010 Ideas'>Social Traffic Analyzer &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/09/comment-on-everything-january-2010-idea/' rel='bookmark' title='Permanent Link: Comment On Everything &#8211; January 2010 Ideas'>Comment On Everything &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/07/app-idea-store-january-2010-idea/' rel='bookmark' title='Permanent Link: App Idea Store &#8211; January 2010 Ideas'>App Idea Store &#8211; January 2010 Ideas</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2010/01/16/copy-paste-injection-january-2010-idea/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Offline News Aggregator &#8211; January 2010 Ideas</title>
		<link>http://www.barklund.org/blog/2010/01/02/offline-news-aggregator-january-2010-idea/</link>
		<comments>http://www.barklund.org/blog/2010/01/02/offline-news-aggregator-january-2010-idea/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 22:31:39 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[January 2010 Ideas]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=530</guid>
		<description><![CDATA[The second idea for my 365 social ideas is news aggregation-based as well as usability-oriented. The idea is a cross between the fascinatingly simple Readability bookmarklet and the recently launched Danish online newspaper &#8220;Ugen&#8221; (Eng: &#8220;The Week&#8221;), which is a downloadable AIR-application for news reading. The idea is to create a bookmarklet, just like the [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/01/27/browser-snapshort-sharing-january-2010-idea/' rel='bookmark' title='Permanent Link: Browser Snapshot Sharing &#8211; January 2010 Ideas'>Browser Snapshot Sharing &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/12/swell-wave-january-2010-idea/' rel='bookmark' title='Permanent Link: Swell Wave &#8211; January 2010 Ideas'>Swell Wave &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/09/comment-on-everything-january-2010-idea/' rel='bookmark' title='Permanent Link: Comment On Everything &#8211; January 2010 Ideas'>Comment On Everything &#8211; January 2010 Ideas</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>The second idea for my <a href="/blog/365ideas/" title="365 Social Ideas from Barklund.org">365 social ideas</a> is news aggregation-based as well as usability-oriented. The idea is a cross between the fascinatingly simple <a href="http://lab.arc90.com/experiments/readability/" title="Readability bookmarklet from Arc90">Readability bookmarklet</a> and the <a href="http://www.ugen.dk/" title="Official home of Ugen">recently launched Danish online newspaper &#8220;Ugen&#8221;</a> (Eng: &#8220;The Week&#8221;), which is a downloadable AIR-application for news reading.</p>
<p><span id="more-530"></span></p>
<p>The idea is to create a bookmarklet, just like the Readability mentioned above, but instead of simply scraping the contents of the article and only present it to the user on-screen (and then all this is lost when he leaves), also connect to his locally downloaded and running News Aggregator, which will then store the URL, headline and body (and maybe the images, that the user selects as interesting and relevant) for offline viewing at any later stage. Imagine it a bit like sharing a link on Facebook, but actually only share it with yourself by copying the full contents and selected images to a local storage, and then read any article that you have found interesting at any later point.</p>
<p>This idea is completely legal, as no server ever scrapes any content from any website and no public display is made of the copy. The &#8220;copy&#8221; is made by the user himself only on his computer to the user&#8217;s own hard drive as if he copy-pasted it into a Word-document (which is always legal if only for personal use). If one wanted to implement a sharing-function in the application, it would however have to link directly to the original URL as the scraped content cannot be shared in a way, that could ever satisfy publishers.</p>
<h4 id="toc-why">Why?</h4>
<p>Reading long articles in your browser is difficult and tiresome &#8211; especially when cramped in between navigation, advertisements and &#8220;layout&#8221;. If this idea was implemented, we could all in a legally safe way copy article contents for local viewing without having the problems of RSS-feeds (most only showing the first paragraphs) or news aggregators (some blocked by publishers).</p>
<h4 id="toc-whats-next">What&#8217;s next?</h4>
<p>Do with this idea whatever you like &#8211; expand, implement, trash or forget. Just remember, that if you use it in anyway make sure to attribute me according to the Creative Commons Attribution 3.0 License, that all these <a href="/blog/365ideas" title="365 Social Ideas from Barklund.org">365 Social Ideas</a> are published under.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/01/27/browser-snapshort-sharing-january-2010-idea/' rel='bookmark' title='Permanent Link: Browser Snapshot Sharing &#8211; January 2010 Ideas'>Browser Snapshot Sharing &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/12/swell-wave-january-2010-idea/' rel='bookmark' title='Permanent Link: Swell Wave &#8211; January 2010 Ideas'>Swell Wave &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/09/comment-on-everything-january-2010-idea/' rel='bookmark' title='Permanent Link: Comment On Everything &#8211; January 2010 Ideas'>Comment On Everything &#8211; January 2010 Ideas</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2010/01/02/offline-news-aggregator-january-2010-idea/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The XKCD book page numbering explained (or skew binary explained)</title>
		<link>http://www.barklund.org/blog/2009/11/24/xkcd-book-page-numbering-explained/</link>
		<comments>http://www.barklund.org/blog/2009/11/24/xkcd-book-page-numbering-explained/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 15:06:19 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Trends]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=473</guid>
		<description><![CDATA[We got the XKCD book &#8220;volume 0&#8243; here at work yesterday, and I have of course skimmed through it many times already. I quickly found the solution to the page numbering scheme, but wanted to see if I could find a simple conversion formula from real number to XKCD page number and vice versa. Introduction [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>We got the <a href="http://store.xkcd.com/xkcd/" title="Buy it if you haven't already - if for nothing else, to support Randall">XKCD book &#8220;volume 0&#8243;</a> <a href="http://konstellation.dk" title="Konstellation.dk - the company website">here at work</a> yesterday, and I have of course skimmed through it many times already. I quickly found the solution to the page numbering scheme, but wanted to see if I could find a simple conversion formula from real number to XKCD page number and vice versa.</p>
<p><span id="more-473"></span></p>
<div class="toc">
<ol>
<li><a href="http://www.barklund.org/blog/2009/11/24/xkcd-book-page-numbering-explained/#toc-introduction">Introduction</a></li>
<li><a href="http://www.barklund.org/blog/2009/11/24/xkcd-book-page-numbering-explained/#toc-conversions">Conversions</a></li>
<li><a href="http://www.barklund.org/blog/2009/11/24/xkcd-book-page-numbering-explained/#toc-properties">Properties</a></li>
<li><a href="http://www.barklund.org/blog/2009/11/24/xkcd-book-page-numbering-explained/#toc-algebra">Algebra</a>
<ol>
<li><a href="http://www.barklund.org/blog/2009/11/24/xkcd-book-page-numbering-explained/#toc-implementation">Implementation</a></li>
</ol>
</li>
<li><a href="http://www.barklund.org/blog/2009/11/24/xkcd-book-page-numbering-explained/#toc-conclusion">Conclusion</a></li>
</ol>
</div>
<h3 id="toc-introduction">Introduction</h3>
<p>In a regular (i.e. useful) number system, the digit values are powers of a given number starting with the right-most digit representing a digit value of the given base number to the power of 0, the second digit from the right representing a digit value of the given base number to the power of 1 etc. This has a lot of advantages beyond the topic of this post. Thus, in a base 10 number system, the right-most digit value is 1, the second from the right is 10, then 100 etc. There is a simple rule for how we go from one digit value to the next and for how to find an arbitrary digit value just by knowing the digit position.</p>
<p>But the simple point I&#8217;m making here is, that we could invent a number system, where the digits represent arbitrary values &#8211; e.g. the first digit from the right represent 1s, the second digit represent 4s, the third represent 178s, the fourth represent 179s etc. We can express any number in this (we could just extend the available numbers with letters or other symbols as we see fit) &#8211; in the example given here, the decimal number <code>2000</code> would be represented as <code>11*179 + 0*178 + 7*4 + 3*1 = B073</code>. It is quite a useless set of digit values, and it is a very hard representational number system to do any math in.</p>
<p>For the XKCD book page numbering, Randall has chosen a number system, where the first digit from the right represent 1s, the second digit represent 3s, the third represent 7s, the fourth represent 15s and so on &#8211; thus there is actually a rule for finding the digit value for the <code>n</code>&#8216;th digit (from the right) = <code>2<sup>i</sup>-1</code> &#8211; compare this to the binary system, in which the digit values can be calculated as <code>2<sup>(i-1)</sup></code>.</p>
<p><strong>UPDATE 2009-11-26:</strong> As <a href="http://www.reddit.com/r/programming/comments/a800q/the_xkcd_book_page_numbering_explained_contains/c0gaav5" title="Part of a reddit comment thread">it has been pointed out to me</a>, this number system (which is skewed one off binary) is called <a href="http://online-judge.uva.es/problemset/v5/575.html">skew binary</a>. The only interesting property that I can find anyone reference is the fact, that increment only ever has one carry-over, which makes increment very easy to implement (and I could thus implement it in regexp as given further below). It is however only a &#8220;scientific&#8221; numbering system, and I can find no reference to any (other) usage in the wild. The article linked before is the first Google hit for &#8220;skew binary&#8221; and it is an 11 year old slide &#8211; which indicates further, that it is a rarely used system with only theoretical purposes.</p>
<h3 id="toc-conversions">Conversions</h3>
<p>So, how to find the value, that a given XKCD page number represent? Simply multiply each digit by the digit value &#8211; if the <code>i</code>&#8216;th digit (where <code>i=1</code> represent the right-most digit) has the value <code>x<sub>i</sub></code> the translation of an <code>n</code> digit XKCD page number can be done via this formula:</p>
<div class="wp-caption aligncenter" style="width: 260px"><img src="http://chart.apis.google.com/chart?cht=tx&#038;chf=bg,s,FFFFFF00&#038;chco=000000&#038;chl=\sum_{i=1}^n x_i (2^i-1)" alt="General conversion rule from XKCD page numbers to decimal numbers" title="General conversion rule from XKCD page numbers to decimal numbers" class="size-full" /><p class="wp-caption-text">General conversion rule from XKCD page numbers to decimal numbers</p></div>
<p>The reverse translation can&#8217;t be done like regular conversion between regular bases bulding the number from the right (using modulus and division), but has to be done from the left. Thus we need to find the maximum <code>i</code> for which <code>2<sup>i</sup>-1</code> is less than the given number &#8211; which we do via <code>log2(number+1)</code>. Then we go from this maximum <code>i</code> (which denotes the number of digits <code>n</code> in the XKCD page number) to 1 and for each step find the <code>i</code>&#8216;th digit by evenly dividing the target number with <code>2<sup>i</sup>-1</code> and storing the remainder as the new target number &#8211; here both implemented in JavaScript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> dec2xkcd<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>d<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>Math.<span style="color: #660066;">LN2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> r<span style="color: #339933;">,</span> x <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     r <span style="color: #339933;">=</span> Math.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
     x <span style="color: #339933;">+=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>d<span style="color: #339933;">/</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     d <span style="color: #339933;">=</span> d<span style="color: #339933;">%</span>r<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> xkcd2dec<span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> l <span style="color: #339933;">=</span> x.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> i <span style="color: #339933;">=</span> l<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    d <span style="color: #339933;">+=</span> parseInt<span style="color: #009900;">&#40;</span>x.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>l<span style="color: #339933;">-</span>i<span style="color: #009900;">&#41;</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: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> d<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We can verify these by running some tests converting back and forth:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> c<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> d_ <span style="color: #339933;">=</span> xkcd2dec<span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> d<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; =&gt; &quot;</span><span style="color: #339933;">+</span>x<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; =&gt; &quot;</span><span style="color: #339933;">+</span>d_<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
c<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: #006600; font-style: italic;">// 1 =&gt; 1 =&gt; 1</span>
c<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 6 =&gt; 20 =&gt; 6</span>
c<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 15 =&gt; 1000 =&gt; 15</span>
c<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 16 =&gt; 1001 =&gt; 16</span>
c<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">119</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 119 =&gt; 111110 =&gt; 119</span>
c<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">120</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 120 =&gt; 111111 =&gt; 120</span>
c<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6542</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 6542 =&gt; 110011001010 =&gt; 6542</span>
c<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5469835</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 5469835 =&gt; 1010011011101101001020 =&gt; 5469835</span></pre></div></div>

<h3 id="toc-properties">Properties</h3>
<p>The selection of digit values are not completely random, they do follow some system, but not the systems of regular power-based number systems. So the properties, that regular power-based number systems have are not present in this, but it does have other properties.</p>
<p>First of all, the notion <code>2^n-1</code> is known from <a href="http://en.wikipedia.org/wiki/Mersenne_prime" title="Mersenne prime on Wikipedia">Mersenne primes</a>. What this tells us however, is nothing useful about the system except that some digit values are prime &#8211; like 3, 7, 31 and maybe, maybe not an infinite number of the infinite other digit values.</p>
<p>Another property is, that from any digit value <code>d<sub>n</sub></code>, the next digit value can always be calculated as <code>2d<sub>n</sub> + 1</code>. This means, that the largest number the can be represented by <code>n</code> digits is a left-most 2 followed by <code>n-1</code> 0s &#8211; the next number in sequence is a left-most 1 followed by <code>n</code> 0s. But again, this is quite trivial and not really that useful.</p>
<p>The <a href="http://en.wikipedia.org/wiki/Repunit">repunit numbers</a> (numbers consisting solely of 1s) in this representational system represent the numbers 1 (<code>1</code>), 4 (<code>11</code>), 11 (<code>111</code>), 26 (<code>1111</code>), 57 (<code>11111</code>) and this order is actually the <a href="http://en.wikipedia.org/wiki/Eulerian_number">second column of the Eulerian numbers</a> (defined by <a href="http://www.research.att.com/~njas/sequences/A000295">sequence A000295 in OEIS</a>) &#8211; but this is still quite useless.</p>
<h3 id="toc-algebra">Algebra</h3>
<p>So, is this usable as a number system? Can we do math in it? Can we in a simple way multiply numbers? Let&#8217;s start by making an algorithm for adding 1 to (incrementing) an arbitrary number. Observe, that we cannot simply safely increment the last digit if it is a zero &#8211; because if the number has a 2 in it (which only occurs in at most one position in the number and with no trailing 1&#8242;s), increment is done by converting the 2 to a 1 and incrementing the digit to the left of the 2 &#8211; examples:</p>
<ul>
<li>Increment 2 => 10</li>
<li>Increment 12 => 20</li>
<li>Increment 102 => 110</li>
<li>Increment 120 => 200</li>
<li>Increment 1012 => 1020</li>
<li>Increment 2000 => 10000</li>
</ul>
<p>It is not trivial, but then again, not that hard. For numbers without a 2, simply increment the right-most digit (which has to be a 0 incremented to a 1 or a 1 incremented to a 2).</p>
<p>Then, how do we subtract 1 (decrement)? Here the rule is, if the number has trailing 0s (one or more right-most digits are 0), decrement the right-most non-zero digit and change the left-most of the trailing 0s (the number just to the right of the right-most non-zero) to a 2 and keep the remaining as 0. If the number does not end in a 0, simply decrement the last digit &#8211; examples:</p>
<ul>
<li>Decrement 2 => 1</li>
<li>Decrement 10 => 2</li>
<li>Decrement 12 => 11</li>
<li>Decrement 112 => 111</li>
<li>Decrement 1010 => 1002</li>
<li>Decrement 1020 => 1012</li>
<li>Decrement 10000 => 2000</li>
</ul>
<p>Still not trivial, but not impossible. <strong>UPDATE 2009-11-26:</strong> The fact that you at most have to alter two connected digits when decrementing or incrementing is what makes this number system interesting as this is a lot easier to implement that normal &#8220;base-based&#8221; number systems, where you worst-case have to alter all digits of the number when in- or decrementing &#8211; compare incrementing the decimal number 999999.</p>
<p>Comparison of numbers is done exactly as for numbers in any (regular) base &#8211; if one has more digits that the other, the &#8220;longest&#8221; number is higher. If not, compare digit by digit starting from the left, if a digit is higher in one number, than the same in the other, that number is larger, if equal, go to next digit. If all digits are the same, the numbers are equal.</p>
<p>All right, so far so good. Then we can add and subtract (but as we won&#8217;t consider negative numbers, subtraction cannot be negative). We add x to y by incrementing y and decrementing x at the same time until x equals 0 &#8211; the value of y is then the result. For subtracting x from y (i.e. <code>y - x</code>), we decrement both numbers until either is zero &#8211; the value of y is then the result (thus 1 &#8211; 2 is 0, as we don&#8217;t do negative number).</p>
<p>The next logical step is to consider multiplication (of positive numbers only of course). We multiply x by y by creating a result variable r and add x to it y times (decrement y each time until 0).</p>
<h4 id="toc-implementation">Implementation</h4>
<blockquote><p>Note: this gets very pointless, so feel free to skip to the conclusion &#8211; which is that all of this is just silly and pointless.</p></blockquote>
<p>First, increment and decrement in JavaScript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> inc<span style="color: #009900;">&#40;</span>x<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: #CC0000;">2</span><span style="color: #339933;">/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>x<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> x
      .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/1$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/0$/</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>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x
    .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/(0|^)2/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;10&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/12/</span> <span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;20&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> dcr<span style="color: #009900;">&#40;</span>x<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: #CC0000;">0</span>$<span style="color: #339933;">/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>x<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> x
      .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/1$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span>
      .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/2$/</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>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x
    .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/20(0*)$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;12$1&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/10(0*)$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;02$1&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^0+/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> id_test<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x2 <span style="color: #339933;">=</span> inc<span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x0 <span style="color: #339933;">=</span> dcr<span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x0<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>xkcd2dec<span style="color: #009900;">&#40;</span>x0<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) =&gt; &quot;</span><span style="color: #339933;">+</span>x<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) =&gt; &quot;</span><span style="color: #339933;">+</span>x2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>xkcd2dec<span style="color: #009900;">&#40;</span>x2<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
id_test<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: #006600; font-style: italic;">// 0 (0) =&gt; 1 (1) =&gt; 2 (2)</span>
id_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// 1 (1) =&gt; 2 (2) =&gt; 10 (3)</span>
id_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// 12 (5) =&gt; 20 (6) =&gt; 100 (7)</span>
id_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// 20 (6) =&gt; 100 (7) =&gt; 101 (8)</span>
id_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">117</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 111100 (116) =&gt; 111101 (117) =&gt; 111102 (118)</span>
id_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">123</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 111120 (122) =&gt; 111200 (123) =&gt; 112000 (124)</span>
id_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">127</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 200000 (126) =&gt; 1000000 (127) =&gt; 1000001 (128)</span>
id_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">128</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 1000000 (127) =&gt; 1000001 (128) =&gt; 1000002 (129)</span>
id_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">603</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 100101120 (602) =&gt; 100101200 (603) =&gt; 100102000 (604)</span></pre></div></div>

<p>Then comparison:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> cmp<span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span>x2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> l <span style="color: #339933;">=</span> x1.<span style="color: #660066;">length</span><span style="color: #339933;">,</span> l2 <span style="color: #339933;">=</span> x2.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>l <span style="color: #339933;">!=</span> l2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> l <span style="color: #339933;">&gt;</span> l2 <span style="color: #339933;">?</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">,</span> c1<span style="color: #339933;">,</span> c2<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</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>l<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    c1 <span style="color: #339933;">=</span> x1.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    c2 <span style="color: #339933;">=</span> x2.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c1 <span style="color: #339933;">&gt;</span> c2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c2 <span style="color: #339933;">&gt;</span> c1<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: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> 
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> cmp_test<span style="color: #009900;">&#40;</span>d1<span style="color: #339933;">,</span> d2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x1 <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x2 <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> cmp<span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> x2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> x1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) is greater than &quot;</span><span style="color: #339933;">+</span>x2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>c <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> x2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) is greater than &quot;</span><span style="color: #339933;">+</span>x1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) equals &quot;</span><span style="color: #339933;">+</span>x2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
cmp_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</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: #006600; font-style: italic;">// 1 (1) equals 1 (1)</span>
cmp_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</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: #006600; font-style: italic;">// 2 (2) is greater than 1 (1)</span>
cmp_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</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: #006600; font-style: italic;">// 2 (2) is greater than 1 (1)</span>
cmp_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">117</span><span style="color: #339933;">,</span><span style="color: #CC0000;">117</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 111101 (117) equals 111101 (117)</span>
cmp_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">117</span><span style="color: #339933;">,</span><span style="color: #CC0000;">121</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 111112 (121) is greater than 111101 (117)</span>
cmp_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">127</span><span style="color: #339933;">,</span><span style="color: #CC0000;">128</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 1000001 (128) is greater than 1000000 (127)</span>
cmp_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">127</span><span style="color: #339933;">,</span><span style="color: #CC0000;">126</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 1000000 (127) is greater than 200000 (126)</span></pre></div></div>

<p>Then addition:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> add<span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> x2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>cmp<span style="color: #009900;">&#40;</span>x2<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    x1 <span style="color: #339933;">=</span> inc<span style="color: #009900;">&#40;</span>x1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    x2 <span style="color: #339933;">=</span> dcr<span style="color: #009900;">&#40;</span>x2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x1<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> add_test<span style="color: #009900;">&#40;</span>d1<span style="color: #339933;">,</span> d2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x1 <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x2 <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> xs <span style="color: #339933;">=</span> add<span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> x2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> ds <span style="color: #339933;">=</span> xkcd2dec<span style="color: #009900;">&#40;</span>xs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) + &quot;</span><span style="color: #339933;">+</span>x2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) = &quot;</span><span style="color: #339933;">+</span>xs<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>ds<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</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: #006600; font-style: italic;">// 1 (1) + 1 (1) = 2 (2)</span>
add_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</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: #006600; font-style: italic;">// 1 (1) + 2 (2) = 10 (3)</span>
add_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</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: #006600; font-style: italic;">// 2 (2) + 1 (1) = 10 (3)</span>
add_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7</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: #006600; font-style: italic;">// 100 (7) + 100 (7) = 200 (14)</span>
add_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">17</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// 101 (8) + 1002 (17) = 1110 (25)</span>
add_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">53</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">17</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 11100 (53) + 1002 (17) = 100100 (70)</span></pre></div></div>

<p>Then subtraction:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> sub<span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> x2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>cmp<span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> cmp<span style="color: #009900;">&#40;</span>x2<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    x1 <span style="color: #339933;">=</span> dcr<span style="color: #009900;">&#40;</span>x1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    x2 <span style="color: #339933;">=</span> dcr<span style="color: #009900;">&#40;</span>x2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x1<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> sub_test<span style="color: #009900;">&#40;</span>d1<span style="color: #339933;">,</span> d2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x1 <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x2 <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> xs <span style="color: #339933;">=</span> sub<span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> x2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> ds <span style="color: #339933;">=</span> xkcd2dec<span style="color: #009900;">&#40;</span>xs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) - &quot;</span><span style="color: #339933;">+</span>x2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) = &quot;</span><span style="color: #339933;">+</span>xs<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>ds<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
sub_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</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: #006600; font-style: italic;">// 1 (1) - 1 (1) = 0 (0)</span>
sub_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</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: #006600; font-style: italic;">// 1 (1) - 2 (2) = 0 (0)</span>
sub_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</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: #006600; font-style: italic;">// 2 (2) - 1 (1) = 1 (1)</span>
sub_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7</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: #006600; font-style: italic;">// 100 (7) - 100 (7) = 0 (0)</span>
sub_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">17</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// 1002 (17) - 101 (8) = 102 (9)</span>
sub_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">53</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">17</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 11100 (53) - 1002 (17) = 10012 (36)</span></pre></div></div>

<p>Finally multiplication:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> mul<span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> x2<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>cmp<span style="color: #009900;">&#40;</span>x2<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> x2<span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// x2 is not zero, go</span>
  <span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> x1<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>cmp<span style="color: #009900;">&#40;</span>x2<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    r <span style="color: #339933;">=</span> add<span style="color: #009900;">&#40;</span>r<span style="color: #339933;">,</span> x1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    x2 <span style="color: #339933;">=</span> dcr<span style="color: #009900;">&#40;</span>x2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> mul_test<span style="color: #009900;">&#40;</span>d1<span style="color: #339933;">,</span> d2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x1 <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> x2 <span style="color: #339933;">=</span> dec2xkcd<span style="color: #009900;">&#40;</span>d2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> xs <span style="color: #339933;">=</span> mul<span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> x2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> ds <span style="color: #339933;">=</span> xkcd2dec<span style="color: #009900;">&#40;</span>xs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> x1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d1<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) * &quot;</span><span style="color: #339933;">+</span>x2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>d2<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;) = &quot;</span><span style="color: #339933;">+</span>xs<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; (&quot;</span><span style="color: #339933;">+</span>ds<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
mul_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</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: #006600; font-style: italic;">// 1 (1) * 1 (1) = 1 (1)</span>
mul_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</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: #006600; font-style: italic;">// 1 (1) * 2 (2) = 2 (2)</span>
mul_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</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: #006600; font-style: italic;">// 2 (2) * 1 (1) = 2 (2)</span>
mul_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7</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: #006600; font-style: italic;">// 100 (7) * 100 (7) = 11010 (49)</span>
mul_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">17</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// 1002 (17) * 101 (8) = 1000102 (136)</span>
mul_test<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">53</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">17</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 11100 (53) * 1002 (17) = 111000101 (901)</span></pre></div></div>

<h3 id="toc-conclusion">Conclusion</h3>
<p>As expected, it is just a silly and very useless number system &#8211; but fun to play around with though. If you however find other interesting properties of this representational system, please feel free to comment.</p>
<p>However, it should be clear to anyone, that this has nothing to do with a <a href="http://en.wikipedia.org/wiki/Ternary_numeral_system" title="Ternary numeral system on Wikipedia">ternary number system</a> just because the only digits are 0, 1 and 2 &#8211; it is closer related to the binary number system.</p>
<p><strong>UPDATE 2009-11-26:</strong> Albeit still useless, the number system actually has the name &#8220;skew binary&#8221; as mentioned in the introduction &#8211; and thus has been the target of some scientific research previously.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/11/24/xkcd-book-page-numbering-explained/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Datasets in HTML 5 and what they&#8217;re good for</title>
		<link>http://www.barklund.org/blog/2009/08/28/html-5-datasets/</link>
		<comments>http://www.barklund.org/blog/2009/08/28/html-5-datasets/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 10:55:12 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[Trends]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=317</guid>
		<description><![CDATA[Datasets are quite an un-impressive part of the HTML 5 specification. It is only accompanied by two examples, of which the first is relevant but small and the latter is (in my view) completely besides the potential of this new functionality. Datasets are in short a way to store custom values on nodes in the [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/08/20/web-sockets-html-5-solve-http-pull/' rel='bookmark' title='Permanent Link: Web Sockets in HTML 5 might solve the http pull problem once and for all'>Web Sockets in HTML 5 might solve the http pull problem once and for all</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Datasets are quite an <a href="http://dev.w3.org/html5/spec/Overview.html#embedding-custom-non-visible-data">un-impressive part of the HTML 5 specification</a>. It is only accompanied by two examples, of which the first is relevant but small and the latter is (in my view) completely besides the potential of this new functionality. Datasets are in short a way to store custom values on nodes in the DOM tree. This has always been possible when working client-side through JavaScript by keeping a mapping between the nodes and the values stored, which meant that the values weren&#8217;t actually stored <em>on</em> the nodes, but just related to the nodes elsewhere (e.g. using <a href="http://docs.jquery.com/Internals/jQuery.data" title="Documentation for the jQuery.data plugin">jQuery.data</a>).</p>
<p>With HTML 5 and the introduction of datasets, we can do something new. The main advantage and the really huge application area for datasets are, that you can include custom attributes in the serverside-rendered HTML, that the client-side script will use to enhance the browsing experience.</p>
<p><span id="more-317"></span></p>
<p>A common example: You have a gallery page, which has an area for displaying a large image and one for a list of thumbnails. In the serverside-rendered HTML you render the first picture in the large image and a list of thumbnails below. In javaScript you hook a function for each thumbnail to change the large image. But where do you supply the path for the large image? It could be done via several methods:
<ul>
<li>from the server render a JavaScript-array that do the mapping,</li>
<li>have a strict naming-convention, and e.g. always remove &#8220;-thumb&#8221; from the thumbnail path to obtain the large image path,</li>
<li>hide it on the node by abusing some existing legal property &#8211; e.g. hide it in the title-field of the thumbnail, or</li>
<li>add it is a new property on the node, that will make your document non-compliant with the standard.</li>
</ul>
<p>But with datasets, the server simply includes the large image path on the thumbnail image node (or the surrounding link node) via a custom data-attribute here named <code>data-fullpath</code>:</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;img</span></span>
<span style="color: #009900;">  <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;large-image&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;images/large/picture1.png&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;Large Picture 1&quot;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;thumbnails&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;gallery_with_picture1_enlarged.html&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;View Picture 1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span></span>
<span style="color: #009900;">        <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;images/thumbs/picture1.png&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">data-fullpath</span>=<span style="color: #ff0000;">&quot;images/large/picture1.png&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;Picture 1&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;gallery_with_picture2_enlarged.html&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;View Picture 2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span></span>
<span style="color: #009900;">        <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;images/thumbs/picture2.png&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">data-fullpath</span>=<span style="color: #ff0000;">&quot;images/large/picture2.png&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;Picture 2&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;gallery_with_picture3_enlarged.html&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;View Picture 3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span></span>
<span style="color: #009900;">        <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;images/thumbs/picture3.png&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">data-fullpath</span>=<span style="color: #ff0000;">&quot;images/large/picture147_no_conventions.png&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;Picture 3&quot;</span></span>
<span style="color: #009900;">      <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><s>Of course, in live implementation the links above would link to pages, where the other thumbnails where shown in the large area to allow for graceful degradation, but this is just to show the idea.</s> The above example have now been updated to provide actually interesting links in the surrounding <code>&lt;a></code>-tags and not just link to the image as several have suggested.</p>
<p>The JavaScript that went with this would be something along the lines of (asuming jQuery.dataset functionality):</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;ul#thumbnails a&quot;</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>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#large-image&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;src&quot;</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;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;img&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dataset</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fullpath'</span><span style="color: #009900;">&#41;</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></pre></div></div>

<p>See <a href="http://barklund.org/examples/html5/datasets/" title="Example page at barklund.org">full example here</a> &#8211; it works in all browsers, and is <a href="http://validator.w3.org/check?uri=http%3A%2F%2Fbarklund.org%2Fexamples%2Fhtml5%2Fdatasets%2F" title="Validation result for example page">valid HTML 5</a>. The JavaScript works in non-HTML5 browsers, as it includes a <a href="http://plugins.jquery.com/project/datasets" title="Description of my plugin for datasets for jQuery">little jQuery plugin I wrote</a> adding the method <code>dataset</code>, which includes a fallback if the DOM attribute &#8220;dataset&#8221; is not available.</p>
<p>I personally find this to be a very clean way of implementing this feature over the many hacks, that we need to use today. And this is of course not limited to galleries, but can be used for anything, where the script needs to know extra information about the objects in the document in order to add additional functionality through scripts on the page &#8211; this could include having a reference to &#8220;previous&#8221; or &#8220;next&#8221; image in the above gallery, meta-information about rows in a table to use for client-side searching, filtering or sorting, meta data for link previews and many other possibilities.</p>
<p>NB: This is <strong>not</strong> to be used as meta-data for external entities &#8211; only for scripts on the page enhancing the browsing experience. For providing meta-data for external entities, use <a href="http://dev.w3.org/html5/spec/Overview.html#microdata" title="The microdata section in the HTML 5 specification">microdata</a>, which is completely different from datasets &#8211; but the two could be used to convey the same meta information, just with different recipients in mind.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/08/20/web-sockets-html-5-solve-http-pull/' rel='bookmark' title='Permanent Link: Web Sockets in HTML 5 might solve the http pull problem once and for all'>Web Sockets in HTML 5 might solve the http pull problem once and for all</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/08/28/html-5-datasets/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Using the regular expression backtracking engine for prime number magic</title>
		<link>http://www.barklund.org/blog/2009/07/16/regular-expresssion-backtracking-prime-magic/</link>
		<comments>http://www.barklund.org/blog/2009/07/16/regular-expresssion-backtracking-prime-magic/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 14:13:28 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Trends]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=212</guid>
		<description><![CDATA[I just saw the most impressive, intelligent use of the regular expression backtracking engine and simply have to re-post it in order to give my own decryption of the infinitely simple expression. In short, it is a regular expression, that only matches non-prime sequences of 1&#8242;s. And it goes like this: /^1?$&#124;^(11+?)\1+$/ I will try [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I just <a href="http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/" title="Avinash Meetoo: Blog » A regular expression to check for prime numbers">saw the most impressive, intelligent use of the regular expression backtracking engine</a> and simply have to re-post it in order to give my own decryption of the infinitely simple expression. In short, it is a regular expression, that only matches non-prime sequences of 1&#8242;s. And it goes like this:</p>

<div class="wp_syntax"><div class="code"><pre class="regexp" style="font-family:monospace;">/^1?$|^(11+?)\1+$/</pre></div></div>

<p>I will try to explain it in a simple way and then do some benchmarking on this simple expression, a slightly optimized version of the expression as well as compare it to more optimal algorithms.</p>
<p><span id="more-212"></span></p>
<p>I can explain it very simply. The first part of the expression check the special cases of zero or one 1&#8242;s. The second part checks whether the number of 1&#8242;s is a multiple of another substring of 1&#8242;s. It does that by un-greedily matching the shortest possible string of 1&#8242;s larger than one (the shortest possible is initially of course two 1&#8242;s) as the first group and then checks whether the remaining number of 1&#8242;s is evenly divisible by this by a factor larger that zero &#8211; that is, if there were six 1&#8242;s, the remaining number of 1&#8242;s after having matched the first two would be four, which is evenly divisible by two by a factor larger than zero. If we tried to match nine 1&#8242;s, the first attempt at matching two 1&#8242;s in the first group would fail, as the remaining seven 1&#8242;s is not evenly divisible by this. But the regular expression backtracking engine would then backtrack to matching three 1&#8242;s in the first group, and then the remaining six 1&#8242;s would be evenly divisible by this number, and the pattern matches &#8211; thus 9 is <strong>not</strong> prime as expected.</p>
<p>The penalty? Well, try to match eleven 1&#8242;s, the first group would initially be set to two 1&#8242;s, and the remaining expression fails, the initial group would then be set to three 1&#8242;s, and the remaining expression would still fail, then the initial expression would be set to four 1&#8242;s (which is just stupid prime-checking-wise &#8211; 2 was not a factor, then how could 4 be?) and it would still fail &#8211; and so on until the engine would match all eleven 1&#8242;s as the first group and check if the remaining zero 1&#8242;s is evenly divisible (by a factor larger than zero of course) which it isn&#8217;t and only then would the expression fail. It is thus highly expensive, as there is of course no need to check if any number higher than the square root of the number to check divides the number evenly, if all numbers below the square root doesn&#8217;t.</p>
<p>To do some speed-testing, let&#8217;s try to check the number 9017 (which is the product of the somewhat large primes 71 and 127 and thus not prime) and the number 9029 (which is next prime after 9017). And for the fun of it, let&#8217;s test it in JavaScript right here in the browser:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> check1<span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t1 <span style="color: #339933;">=</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;">getTime</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> str <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</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>number<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> str <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t2 <span style="color: #339933;">=</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;">getTime</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> result <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^1?$|^(11+?)\1+$/</span>.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t3 <span style="color: #339933;">=</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;">getTime</span><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: #3366CC;">&quot;Time of generation: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t2<span style="color: #339933;">-</span>t1<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, time of checking: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t3<span style="color: #339933;">-</span>t2<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, result: &quot;</span><span style="color: #339933;">+</span>number<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; is &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>result <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;not &quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;prime&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
check1<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9017</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of generation: 2 ms, time of checking: 6 ms, result: 9017 is not prime&quot;</span>
check1<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9029</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of generation: 2 ms, time of checking: 64 ms, result: 9029 is prime&quot;</span></pre></div></div>

<p>64 ms for failing to validate 9029 as not prime is way too long, so let&#8217;s simply change &#8220;(11+?)&#8221; to &#8220;(1{2,x}?)&#8221; with x being the square root of the given number truncated to an integer:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> check2<span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t1 <span style="color: #339933;">=</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;">getTime</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> str <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</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>number<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> str <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;1&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> sqrt <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> re <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> RegExp<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;^1?$|^(1{2,&quot;</span><span style="color: #339933;">+</span>sqrt<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;}?)<span style="color: #000099; font-weight: bold;">\\</span>1+$&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t2 <span style="color: #339933;">=</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;">getTime</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> result <span style="color: #339933;">=</span> re.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t3 <span style="color: #339933;">=</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;">getTime</span><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: #3366CC;">&quot;Time of generation: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t2<span style="color: #339933;">-</span>t1<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, time of checking: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t3<span style="color: #339933;">-</span>t2<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, result: &quot;</span><span style="color: #339933;">+</span>number<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; is &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>result <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;not &quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;prime&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
check2<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9017</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of generation: 2 ms, time of checking: 11 ms, result: 9017 is not prime&quot;</span>
check2<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9029</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of generation: 2 ms, time of checking: 12 ms, result: 9029 is prime&quot;</span></pre></div></div>

<p>This actually increased the time to check for non-primes (as &#8220;{a,b}?&#8221; is more expensive than simply &#8220;+?&#8221; as the recursion needs to keep track of the limits) but greatly reduced the time it took to check for primes, as it only checks up until 95 1&#8242;s (95 = floor(sqrt(9029))) and not all the way up to 9029 1&#8242;s.</p>
<p>But just for the fun of it, let&#8217;s compare with more common check&#8217;s &#8211; like <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" title="Sieve of Eratosthenes on the English Wikipedia">Sieve of Eratosthenes</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> check3<span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t1 <span style="color: #339933;">=</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;">getTime</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> nonprimes <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> sqrt <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> pointer <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>pointer <span style="color: #339933;">&lt;=</span> sqrt <span style="color: #339933;">&amp;&amp;</span> nonprimes<span style="color: #009900;">&#91;</span>number<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">true</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: #339933;">=</span> pointer<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> number<span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> pointer<span style="color: #009900;">&#41;</span> nonprimes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>pointer <span style="color: #339933;">&lt;=</span> sqrt <span style="color: #339933;">&amp;&amp;</span> nonprimes<span style="color: #009900;">&#91;</span>pointer<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">var</span> result <span style="color: #339933;">=</span> nonprimes<span style="color: #009900;">&#91;</span>number<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> t2 <span style="color: #339933;">=</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;">getTime</span><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: #3366CC;">&quot;Time of checking: &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>t2<span style="color: #339933;">-</span>t1<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; ms, result: &quot;</span><span style="color: #339933;">+</span>number<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; is &quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>result <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;not &quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;prime&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
check3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9017</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of checking: 1 ms, result: 9017 is not prime&quot;</span>
check3<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">9029</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;Time of checking: 1 ms, result: 9029 is prime&quot;</span></pre></div></div>

<p>It is pretty clear, that the regular expression does not in any way compare to an optimal algorithm &#8211; but the original regular expression referenced in the top is none the less a very intelligent hack on the regular expression engine mechanics.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/07/16/regular-expresssion-backtracking-prime-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using AddThis with Flash</title>
		<link>http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/</link>
		<comments>http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 12:25:36 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Mashup]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=114</guid>
		<description><![CDATA[<p>As I have posted recently, my (former) <a href="http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/" title="Using ShareThis with Flash from Barklund.org">sharing service of choice was ShareThis</a>. No really good reason for that choice actually, just came by their service first, and it worked like a charm. But now it doesn't anymore, so I looked around for alternatives.</p>
<p>The largest such (might even be larger than <a href="http://sharethis.com/" title="Homepage of ShareThis">ShareThis</a>) is <a href="http://addthis.com/" title="Homepage of AddThis">AddThis</a>. Their API is incredibly simple, as you can actually share anything by giving a simply link to their site with url and title of what to share.<p>


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/' rel='bookmark' title='Permanent Link: Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients'>Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients</a></li>
<li><a href='http://www.barklund.org/blog/2009/10/21/bitmap-fill-crash-flash-cs4/' rel='bookmark' title='Permanent Link: Weird bitmap fill crash error in Flash CS4.'>Weird bitmap fill crash error in Flash CS4.</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/06/flash-development-scripted-layout/' rel='bookmark' title='Permanent Link: Flash development and the practice of Scripted Layout'>Flash development and the practice of Scripted Layout</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>As I have posted recently, my (former) <a href="http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/" title="Using ShareThis with Flash from Barklund.org">sharing service of choice was ShareThis</a>. No really good reason for that choice actually, just came by their service first, and it worked like a charm. But now it doesn&#8217;t anymore (update: well it does again, but there were problems), so I looked around for alternatives.</p>
<p>The largest such (might even be larger than <a href="http://sharethis.com/" title="Homepage of ShareThis">ShareThis</a>) is <a href="http://addthis.com/" title="Homepage of AddThis">AddThis</a>. Their API is incredibly simple, as you can actually share anything by giving a simply link to their site with url and title of what to share.
<p>
<span id="more-114"></span></p>
<p>The simplest way to share via AddThis is by opening a link like this:</p>
<blockquote><p><a href="http://www.addthis.com/bookmark.php?v=250&#038;url=http%3A%2F%2Fwww.barklund.org%2Fblog%2F2009%2F06%2F08%2Fusing-addthis-with-flash%2F&#038;title=Barklund.org%20-%20Using%20AddThis%20with%20Flash" title="A link to share this post using AddThis" target="_blank">http://www.addthis.com/bookmark.php?v=250&#038;url=http%3A%2F%2Fwww.barklund.org%2Fblog%2F2009%2F06%2F08%2Fusing-addthis-with-flash%2F&#038;title=Barklund.org%20-%20Using%20AddThis%20with%20Flash</a></p></blockquote>
<p>Integrating this into an example like my previous for ShareThis is then of course very simple as well &#8211; encode URL and title, append to URL of AddThis bookmarking page and open this page in a popup of the proper size (found by experiment). See example <a href="http://www.barklund.org/examples/addthis/" title="Examples using AddThis with Flash">here</a>.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/' rel='bookmark' title='Permanent Link: Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients'>Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients</a></li>
<li><a href='http://www.barklund.org/blog/2009/10/21/bitmap-fill-crash-flash-cs4/' rel='bookmark' title='Permanent Link: Weird bitmap fill crash error in Flash CS4.'>Weird bitmap fill crash error in Flash CS4.</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/06/flash-development-scripted-layout/' rel='bookmark' title='Permanent Link: Flash development and the practice of Scripted Layout'>Flash development and the practice of Scripted Layout</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Google&#8217;s JavaScript API &#8211; Can we have SWFAddress as well?</title>
		<link>http://www.barklund.org/blog/2009/05/26/googles-javascript-api-swfaddress/</link>
		<comments>http://www.barklund.org/blog/2009/05/26/googles-javascript-api-swfaddress/#comments</comments>
		<pubDate>Tue, 26 May 2009 13:35:19 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=103</guid>
		<description><![CDATA[I have recently become a fan of Google&#8217;s very simple JavaScript API. The sole purpose of this project is to avoid all of us including common libraries like SWFObject or jQuery in every project we create and instead load it from a central (updated) repository. I have since used this in many projects. However, who [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/08/copenhagen-google-earth/' rel='bookmark' title='Permanent Link: Copenhagen on Google Earth'>Copenhagen on Google Earth</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/23/google-docs-application-january-2010-idea/' rel='bookmark' title='Permanent Link: Google Docs Application &#8211; January 2010 Ideas'>Google Docs Application &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/02/12/vimeo-vulnerable-csrf/' rel='bookmark' title='Permanent Link: Vimeo highly vulnerable to CSRF attacks &#8211; now fixed'>Vimeo highly vulnerable to CSRF attacks &#8211; now fixed</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I have recently become a fan of <a href="http://code.google.com/apis/ajaxlibs/" title="Google AJAX Libraries">Google&#8217;s very simple JavaScript API</a>. The sole purpose of this project is to avoid all of us including common libraries like <a href="http://code.google.com/p/swfobject/" title="SWFObject project page on code.google.com">SWFObject</a> or <a href="http://jquery.com/" title="Homepage of jQuery">jQuery</a> in every project we create and instead load it from a central (updated) repository. I have since used this in many projects.</p>
<p>However, who decides which libraries to include and how common should a library be, before Google will include it? Personally I believe they should add <a href="http://www.asual.com/swfaddress/" title="Homepage of SWFAddress on asual.com">SWFAddress</a> to the list of supported libraries as well.</p>
<p><span id="more-103"></span></p>
<p>You can however still load SWFObject from the Google JavaScript API and SWFAddress from a local file without any problems. It is quite straightforward:</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>
<span style="color: #009900;">        <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js&quot;</span>&gt;</span>
<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> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/swfaddress.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>
	swfobject.embedSWF(&quot;file.swf&quot;, &quot;target&quot;, &quot;100%&quot;, &quot;100%&quot;, &quot;9.0.0&quot;);
<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>Simply including the SWFAddress library after the Google JS API script works like expected.</p>
<p>But what is the potential consequences or drawbacks from doing this &ndash; depending on Google for serving site-breaking libraries? Some note, that this <a href="http://cxliv.org/2008/05/27/should_you_use_googles_ajax_libraries_api_for_hosting.php" title="Should You Use Google's Ajax Libraries API for Hosting? from cxliv.org">will increase load time</a>, as the user&#8217;s browser now has to create a new connection to a new domain, do DNS lookup and the whole thing. I believe, that when enough sites use the libraries this way, every user will soon have the libraries cached, and when they visit your site for the first time, they will already have all the libraries needed and can thus use the page several seconds faster than if they had to download yet another copy of jQuery to their local cache.</p>
<p>And of course, I have to mention the possibility, that if Google goes out of business, your sites will break. But, it is way more likely that all your libraries will go defunct before Google kicks the bucket, so I believe that to be the least or your worries.</p>
<p>Please share your thoughts on whether it is at all meaningful to use Google JavaScript API and if so, how to determine or influence which libraries they should support.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/08/copenhagen-google-earth/' rel='bookmark' title='Permanent Link: Copenhagen on Google Earth'>Copenhagen on Google Earth</a></li>
<li><a href='http://www.barklund.org/blog/2010/01/23/google-docs-application-january-2010-idea/' rel='bookmark' title='Permanent Link: Google Docs Application &#8211; January 2010 Ideas'>Google Docs Application &#8211; January 2010 Ideas</a></li>
<li><a href='http://www.barklund.org/blog/2010/02/12/vimeo-vulnerable-csrf/' rel='bookmark' title='Permanent Link: Vimeo highly vulnerable to CSRF attacks &#8211; now fixed'>Vimeo highly vulnerable to CSRF attacks &#8211; now fixed</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/05/26/googles-javascript-api-swfaddress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using ShareThis with Flash</title>
		<link>http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/</link>
		<comments>http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/#comments</comments>
		<pubDate>Wed, 06 May 2009 12:44:50 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/</guid>
		<description><![CDATA[This feature does currently not work &#8211; see list of updates below ShareThis is a great service for adding shareability to your website in a very simple way. It even includes the &#8220;tip a friend&#8221; option, so you don&#8217;t need to code that for your own site. I will describe how to embed it from [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/' rel='bookmark' title='Permanent Link: Using AddThis with Flash'>Using AddThis with Flash</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/01/minor-change-in-handling-nan-positions-in-flash-player-10-0-22/' rel='bookmark' title='Permanent Link: Minor change in handling NaN positions in Flash Player 10,0,22'>Minor change in handling NaN positions in Flash Player 10,0,22</a></li>
<li><a href='http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/' rel='bookmark' title='Permanent Link: Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients'>Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<blockquote style="background: #333; font-size: 120%; padding: 5px;"><p>This feature does currently not work &#8211; see list of updates below</p></blockquote>
<p>
<a href="http://www.sharethis.com" title="ShareThis website">ShareThis</a> is a great service for adding shareability to your website in a very simple way. It even includes the &#8220;tip a friend&#8221; option, so you don&#8217;t need to code that for your own site. I will describe how to embed it from flash.
</p>
<p><strong>Note 2009/05/19: </strong>Due to changes in the ShareThis API popup-functionality is broken currently (both in my example and in some of my live projects). I have filed a bug with ShareThis to fix this ASAP.</p>
<p><strong>Note 2009/05/21: </strong>ShareThis have now fixed the above bug but there is still a minor JavaScript error thrown. The popup works now though as it should from both JS and Flash.</p>
<p><strong>Note 2009/06/08: </strong>ShareThis now broke it again. And this time their support staff doesn&#8217;t answer. Please see <a href="http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/" title="Using AddThis with Flash from Barklund.org">an example implemented with AddThis</a> instead.</p>
<p><strong>Note 2009/06/19: </strong>ShareThis finally fixed it again. They wrote a mail on the 17th to me stating the bug was reintroduced when they implemented CDN, but would be fixed in the next release. Let&#8217;s hope it stays that way.</p>
<p><strong>Note 2009/08/14: </strong>ShareThis has recently implemented a brand new version of their service &#8211; and surprise: it is <strong>not</strong> backwards compatible. Thus my popup example is broken again. I will investigate and fix soon.</p>
<p><strong>Note 2009/08/17: </strong>ShareThis has promised to deliver a flash API within a few weeks. Let&#8217;s see what happens then&#8230;</p>
<p><span id="more-48"></span></p>
<p>
You can do that in two ways, either by using a overlay div in HTML or creating a popup (that might get blocked). The prettiest and most user friendly is of course the overlay, but in order to have HTML overlays on top of Flash elements, you need to set the Flash <a href="http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&#038;file=00000852.html" title="Adobe Livedocs: wmode attribute/parameter description">wmode to transparent</a>. This has a strange and unfortunate side-effect (which has been <a href="http://bugs.adobe.com/jira/browse/FP-479" title="Adobe bug-report - it is marked as closed, but it is not resolved">bug-reported to Adobe</a>), that the keyboard defaults to standard US layout. For US websites, this is not a big problem, but for a Danish website it matters a lot. There are many differences between US and DK keyboard layout including Danish special characters and the location of the @-sign. Thus, wmode-transparent cannot be used for Flash-objects having input fields requiring users to enter text (which could contain special characters) or an email address (which contains the @-sign). Thus we need to use the popup-solution.
</p>
<p>
First, simply go to <a href="http://sharethis.com/publishers/" title="Publisher guide to ShareThis">sharethis.com</a> publisher section and <a href="http://sharethis.com/publishers/getbutton/" title="Get the Button">customize your widget</a>. Instead of placing the widget code snippet where you want the button, put it in the &lt;head&gt;-section of your website.  The code snippet I got was:
</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://w.sharethis.com/button/sharethis.js#publisher=636e055b-a4a2-4f9c-872c-b7aa9a701bb0&amp;amp;type=website&amp;amp;send_services=email&amp;amp;post_services=facebook%2Clinkedin%2Cmyspace%2Cdigg%2Cdelicious%2Ctwitter%2Creddit%2Ctechnorati%2Cwordpress%2Cblogger%2Cgoogle_bmarks%2Cwindows_live&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>
Then create a little javascript function like:
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> share<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> title<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> SHARETHIS.<span style="color: #660066;">addEntry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      url<span style="color: #339933;">:</span> url<span style="color: #339933;">,</span>
      title<span style="color: #339933;">:</span> title
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>button<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>popup<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    s.<span style="color: #660066;">popup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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></pre></div></div>

<p>
Finally, simple call this function from Flash using <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html" title="ExternalInterface Livedocs documentation for ActionScript 3">ExternalInterface</a>:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> title:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Barklund.org - Using ShareThis with Flash&quot;</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">external</span>.<span style="color: #006600;">ExternalInterface</span>;
ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;share&quot;</span>, <span style="color: #0066CC;">url</span>, title<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>
See an example <a href="/examples/sharethis/" title="Examples using ShareThis with Flash">here</a> &#8211; the javascript function is invoked both from a regular HTML link and from a simple Flash file using just the above ActionScript.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/' rel='bookmark' title='Permanent Link: Using AddThis with Flash'>Using AddThis with Flash</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/01/minor-change-in-handling-nan-positions-in-flash-player-10-0-22/' rel='bookmark' title='Permanent Link: Minor change in handling NaN positions in Flash Player 10,0,22'>Minor change in handling NaN positions in Flash Player 10,0,22</a></li>
<li><a href='http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/' rel='bookmark' title='Permanent Link: Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients'>Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
