<?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>Jeff Mesnil &#187; javascript</title>
	<atom:link href="http://jmesnil.net/weblog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://jmesnil.net/weblog</link>
	<description>Thoughts about Java, Web and Software Development</description>
	<lastBuildDate>Wed, 25 Jan 2012 10:23:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Chromium Blog: A New Crankshaft for V8 &#9755;</title>
		<link>http://blog.chromium.org/2010/12/new-crankshaft-for-v8.html</link>
		<comments>http://jmesnil.net/weblog/2010/12/07/chromium-blog-a-new-crankshaft-for-v8/#comments</comments>
		<pubDate>Tue, 07 Dec 2010 20:36:59 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jmesnil.net/weblog/?p=1505</guid>
		<description><![CDATA[Today we are introducing Crankshaft, a new compilation infrastructure for V8, Google Chrome’s JavaScript engine. By using aggressive optimizations, Crankshaft dramatically improves the performance of compute-intensive JavaScript applications &#8211; often by more than a factor of two! This will give users a faster and more responsive experience loading web pages and applications built with complex [...]<a href="http://jmesnil.net/weblog/2010/12/07/chromium-blog-a-new-crankshaft-for-v8/" rel="bookmark" title="Permanent link to 'Chromium Blog: A New Crankshaft for V8'" class="glyph">&#9873;</a>
]]></description>
			<content:encoded><![CDATA[<blockquote>
  <p>Today we are introducing Crankshaft, a new compilation infrastructure for V8, Google Chrome’s JavaScript engine. By using aggressive optimizations, Crankshaft dramatically improves the performance of compute-intensive JavaScript applications &#8211; often by more than a factor of two! This will give users a faster and more responsive experience loading web pages and applications built with complex JavaScript.</p>
</blockquote>

<p>Since <a href="http://nodejs.org">node.js</a> is based on V8, I wonder if it can leverage this new infrastructure to improve its performance.</p>
<p><a href="http://jmesnil.net/weblog/2010/12/07/chromium-blog-a-new-crankshaft-for-v8/" rel="bookmark" title="Permanent link to 'Chromium Blog: A New Crankshaft for V8'" class="glyph">&#9873;</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2010/12/07/chromium-blog-a-new-crankshaft-for-v8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSApp.US: a node.js Hosting Provider &#9755;</title>
		<link>http://jsapp.us/</link>
		<comments>http://jmesnil.net/weblog/2010/11/27/jsapp-us-a-node-js-hosting-provider/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 23:56:29 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://jmesnil.net/weblog/?p=1414</guid>
		<description><![CDATA[I was looking for a provider to host the HTML5 / node.js Web application that I am blogging about here and there and JSApp.US is the first one that I found. It is straightforward to use and I have deployed my Web application on it. You can try it by yourself on your iPhone or [...]<a href="http://jmesnil.net/weblog/2010/11/27/jsapp-us-a-node-js-hosting-provider/" rel="bookmark" title="Permanent link to 'JSApp.US: a node.js Hosting Provider'" class="glyph">&#9873;</a>
]]></description>
			<content:encoded><![CDATA[<p>I was looking for a provider to host the HTML5 / <a href="http://nodejs.org/">node.js</a> Web application that I am blogging about <a href="/weblog/2010/11/24/html5-web-application-for-iphone-and-ipad-with-node-js/">here</a> and <a href="/weblog/2010/11/26/offline-support-and-standalone-mode-for-html5-web-application/">there</a> and <a href="http://jsapp.us/">JSApp.US</a> is the first one that I found.</p>

<p>It is straightforward to use and I have deployed my Web application on it. You can try it by yourself on your iPhone or iPad by going to <a href="http://board.jsapp.us/">http://board.jsapp.us/</a>.</p>

<p>At first, it complained that <code>__dirname</code> was not defined when <code>server.js</code> is trying to read files based on the URL path.
I modified <code>server.js</code> to remove the leading <code>/</code> from the path and it works fine:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// won't work on JSApp.us</span>
fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span>__dirname <span style="color: #339933;">+</span> path<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>err<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   ...
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">// but this works</span>
fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span>path.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> path.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>err<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   ...
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>The offline support does not work: somehow this provider does not return the <code>Content-Type</code> HTTP header set by <code>server.js</code>.
Since the cache manifest is not served with the expected <code>text/cache-manifest</code>, I suppose that Safari does not take it into account and bypass the offline configuration.</p>

<p><strong>update:</strong> I was wrong the <code>cache.manifest</code> file is returned with the correct <code>Content-Type</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ curl <span style="color: #660033;">-Gi</span> http:<span style="color: #000000; font-weight: bold;">//</span>board.jsapp.us<span style="color: #000000; font-weight: bold;">/</span>cache.manifest
HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.1</span> <span style="color: #000000;">200</span> OK
Server: nginx<span style="color: #000000; font-weight: bold;">/</span>0.8.53
Date: Sat, <span style="color: #000000;">27</span> Nov <span style="color: #000000;">2010</span> 00:06:<span style="color: #000000;">45</span> GMT
Content-Type: text<span style="color: #000000; font-weight: bold;">/</span>cache-manifest
Transfer-Encoding: chunked
Connection: keep-alive
&nbsp;
CACHE MANIFEST
<span style="color: #666666; font-style: italic;"># 2010-11-27</span>
&nbsp;
CACHE:
<span style="color: #000000; font-weight: bold;">/</span>screen.css
<span style="color: #000000; font-weight: bold;">/</span>client.js
<span style="color: #000000; font-weight: bold;">/</span>jquery.min.js</pre></div></div>


<p>I don&#8217;t know why offline support is not working&#8230;
The rest of the application seems to work fine though.</p>

<p>It will make the next version of the application more interesting. I want to add Web Sockets support to display the pieces of the other users on the Web application (in addition to its own piece).
With this hosting provider, I&#8217;ll be able to release a version that everyone will be able to try.</p>

<p>As an aside, it uses <a href="https://mozillalabs.com/skywriter/">SkyWriter</a> (n&eacute; Bespin) to edit the code and the integration is pretty slick.</p>
<p><a href="http://jmesnil.net/weblog/2010/11/27/jsapp-us-a-node-js-hosting-provider/" rel="bookmark" title="Permanent link to 'JSApp.US: a node.js Hosting Provider'" class="glyph">&#9873;</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2010/11/27/jsapp-us-a-node-js-hosting-provider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#9873; Offline Support and Standalone Mode for HTML5 Web Application</title>
		<link>http://jmesnil.net/weblog/2010/11/26/offline-support-and-standalone-mode-for-html5-web-application/</link>
		<comments>http://jmesnil.net/weblog/2010/11/26/offline-support-and-standalone-mode-for-html5-web-application/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 20:54:28 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://jmesnil.net/weblog/?p=1376</guid>
		<description><![CDATA[Let&#8217;s continue working on the HTML5 Web Application for the iPhone with node.js Last time, I created the first version of the Web application to move a piece when the user moves the iPhone and coded a simple Web server on top of node.js. The Web application works fine but it is not pretty and [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s continue working on the HTML5 Web Application for the iPhone with node.js</p>

<p><a href="http://jmesnil.net/weblog/2010/11/24/html5-web-application-for-iphone-and-ipad-with-node-js/">Last time</a>, I created the first version of the Web application to move a piece when the user moves the iPhone and coded a simple Web server on top of node.js.</p>

<p>The Web application works fine but it is not pretty and there are a few things we can improve.
Before looking at Web Sockets and other fancy code, let&#8217;s make the Web application prettier.</p>

<h2>iPhone Standalone Web Application</h2>

<p>The major annoyance is that Safari chrome takes a lot of space and hides the bottom of the board:</p>

<p><img src="http://jmesnil.net/weblog/wp-content/uploads/2010/11/chrome.png" alt="" title="chrome" width="320" height="155" class="aligncenter size-full wp-image-1383" /></p>

<p>To display a Web application without Safari chrome, we can make it a <a href="http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html%23//apple_ref/doc/uid/TP40002051-CH3-SW2">standalone Web application</a>.</p>

<p>In <code>index.html</code>, we add some <code>&lt;meta&gt;</code> information to the <code>&lt;head&gt;</code> part:</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;">head</span>&gt;</span>
  ...
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;viewport&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width=device-width, user-scalable=0&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;apple-mobile-web-app-capable&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;yes&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;apple-mobile-web-app-status-bar-style&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;black&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span></pre></div></div>


<p>The <a href="http://developer.apple.com/library/safari/documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html#//apple_ref/doc/uid/TP40006509-SW1"><code>viewport</code></a> meta ensures that the Web page will be sized to the device width and prevents the user to change the page scale (by pinching it).</p>

<p>The <a href="http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html%23//apple_ref/doc/uid/TP40002051-CH3-SW2"><code>apple-mobile-web-app-capable</code></a> will launch the Web application in full-screen mode (without Safari chrome) to look like a native application. The Web application must be added to the iPhone home screen and start from there to appear in full-screen mode.</p>

<p>Last thing is to render the iPhone status bar in black by setting the <a href="http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html%23//apple_ref/doc/uid/TP40002051-CH3-SW1"><code>apple-mobile-web-app-status-bar-style</code></a> to black.</p>

<p>With these 3 meta information, the Web application looks better and display the whole board</p>

<p><img src="http://jmesnil.net/weblog/wp-content/uploads/2010/11/Board-2nd-version.jpg" alt="" title="Board - 2nd version" width="320" height="480" class="aligncenter size-full wp-image-1380" /></p>

<h2>Offline Support</h2>

<p>At the moment, node.js serves only static pages. When the iPhone has loaded the Web application, it no longer connects to the Web server.</p>

<p>Let&#8217;s add offline support so that we can run the Web application even when the server is down (or unreachable).</p>

<p>HTML5 supports <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html">offline</a> applications through the cache manifest. The best introduction on the subject is the <a href="http://diveintohtml5.org/offline.html">Offline chapter in Dive into HTML5</a>.</p>

<p>For this Web application this means that we must write a <code>cache.manifest</code> file:</p>


<div class="wp_syntax"><div class="code"><pre class="manifest" style="font-family:monospace;">CACHE MANIFEST
&nbsp;
CACHE:
/screen.css
/client.js
/jquery.min.js</pre></div></div>


<p>This cache manifest tells the Web browsers to call all these files to run the Web application offline.</p>

<p>This cache manifest must be declared in the entry point of the Web application (<code>index.html</code> in this case) with the <code>manifest</code> attribute of the <code>html</code> element:</p>


<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;!doctype html&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> manifest<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/cache.manifest&quot;</span>&gt;</span>
...</pre></div></div>


<p>Finally, we need to update <code>server.js</code> to send the <code>cache.manifest</code> file with the correct HTTP <code>Content-Type</code>.
Only the <code>getContentType()</code>function needs to be modified:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> contentType<span style="color: #009900;">&#40;</span>path<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>path.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.js$'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>path.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.css$'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span>  <span style="color: #3366CC;">&quot;text/css&quot;</span><span style="color: #339933;">;</span>           
   <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>path.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.manifest$'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span>  <span style="color: #3366CC;">&quot;text/cache-manifest&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;text/html&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>With this modification, <code>cache.manifest</code> will be sent to the Web browser with the correct <code>text/cache-manifest</code> Content-Type.</p>

<p>We must restart the server to take into account the <code>server.js</code> modification:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ node.js server.js
HTTP server running at htpp:<span style="color: #000000; font-weight: bold;">//</span>0.0.0.0:<span style="color: #000000;">8080</span></pre></div></div>


<p>I can then reload the Web application and add it to the iPhone home screen.
With offline support, I will be able to use the Web application even if the server is not running or unreachable.</p>

<p>Here is a small video of the result with the updated Web application:</p>

<p><object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/yoJAC2Qc0uo?fs=1&amp;hl=fr_FR&amp;rel=0&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/yoJAC2Qc0uo?fs=1&amp;hl=fr_FR&amp;rel=0&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object></p>

<p>Note the black status bar and the full screen mode after I started the Web application from the iPhone home screen. It is indistinguishable from a native application but runs on the Web (it’s not obvious from the video but the camera was recording from above: the iPhone was held horizontally)</p>

<h2>Source Code</h2>

<p>I have pushed the application on <a href="http://github.com/jmesnil/board-node">Github</a>. You can clone it with Git:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>jmesnil<span style="color: #000000; font-weight: bold;">/</span>board-node.git</pre></div></div>


<h2>Conclusion</h2>

<p>By adding a few <code>meta</code> information to the Web application, it is possible to make it look better integrated to the iPhone.<br />
Offline support is trickier than this simple example make it look like but it gives the right idea to start dealing with it.</p>

<p>Next step is to add Web Sockets support to the node.js to display the moving pieces of <em>other users</em> on the Web application.</p>

<h2>Further Reading</h2>

<ul>
<li><a href="http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/Introduction/Introduction.html">Safari Web Content Guide</a></li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html">HTML5 Offline Web applications</a> (and its corresponding <a href="http://diveintohtml5.org/offline.html">&#8220;Dive into HTML5&#8243; chapter</a>)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2010/11/26/offline-support-and-standalone-mode-for-html5-web-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#9873; HTML5 Web Application for iPhone and iPad With node.js</title>
		<link>http://jmesnil.net/weblog/2010/11/24/html5-web-application-for-iphone-and-ipad-with-node-js/</link>
		<comments>http://jmesnil.net/weblog/2010/11/24/html5-web-application-for-iphone-and-ipad-with-node-js/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 21:38:55 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://jmesnil.net/weblog/?p=1333</guid>
		<description><![CDATA[Update: I have written another post about adding offline support to the Web application and making it look like a native iPhone application. On the server, I want to learn more about node.js which is an interesting way to develop server-side applications. On the client-side, I also wanted to check what can be done with [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> <em>I have written <a href="/weblog/2010/11/26/offline-support-and-standalone-mode-for-html5-web-application/">another post</a> about adding offline support to the Web application and making it look like a native iPhone application.</em></p>

<p>On the server, I want to learn more about <a href="http://nodejs.org/">node.js</a> which is an interesting way to develop server-side applications.
On the client-side, I also wanted to check what can be done with HTML5 on iPhone and iPad with the release of iOS 4.2.1.</p>

<p>I will write a simple web application to do both at the same time. The idea is simple enough but will allow me to dive into node.js and HTML5 quite extensively.</p>

<p>The idea is to write a web application that display a piece moving on a board when the user moves its device. 
Later on, I will expand it so that the user will also see the pieces of all others users connected to the server.</p>

<p>But the first iteration will be enough to familiarize with:</p>

<ul>
<li>node.js (to serve static content at first)</li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">Canvas API</a> (to draw the board and the piece)</li>
<li><a href="http://dev.w3.org/geo/api/spec-source-orientation.html">DeviceOrientation Event API</a> (to detect the orientation of the mobile, it was added in iOS 4.2.1)</li>
</ul>

<h1>Web Server</h1>

<p>I will use node.js as my Web server (I installed it from Git by following the <a href="http://nodejs.org/#build">build instructions</a> on its web site).</p>

<p>The first version of the server needs to serve static files with the correct HTTP <code>Content-Type</code>.
At first, it will serve only HTML pages, CSS stylesheets and JavaScript files.</p>

<p>The whole <code>server.js</code> code is:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'fs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sys'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// the HTTP server</span>
<span style="color: #003366; font-weight: bold;">var</span> server<span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// the HTTP port</span>
<span style="color: #003366; font-weight: bold;">var</span> port <span style="color: #339933;">=</span> <span style="color: #CC0000;">8080</span><span style="color: #339933;">;</span>
&nbsp;
server <span style="color: #339933;">=</span> http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> url.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pathname</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>path <span style="color: #339933;">==</span> <span style="color: #3366CC;">'/'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     path <span style="color: #339933;">=</span> <span style="color: #3366CC;">'/index.html'</span>
  <span style="color: #009900;">&#125;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;serving &quot;</span> <span style="color: #339933;">+</span> path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  fs.<span style="color: #660066;">readFile</span><span style="color: #009900;">&#40;</span>__dirname <span style="color: #339933;">+</span> path<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>err<span style="color: #339933;">,</span> data<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>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">404</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
     <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        res.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span> contentType<span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        res.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #3366CC;">'utf8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        res.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> contentType<span style="color: #009900;">&#40;</span>path<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>path.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.js$'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>path.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.css$'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span>  <span style="color: #3366CC;">&quot;text/css&quot;</span><span style="color: #339933;">;</span>           
   <span style="color: #009900;">&#125;</span>  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;text/html&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
server.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>port<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;HTTP server running at htpp://0.0.0.0:&quot;</span> <span style="color: #339933;">+</span> port <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>The code is straightforward: when a request is handled by the server, it looks in the current directory (where <code>server.js</code> is) for a file with the given path and writes its content in the HTTP response body.<br />
The <code>contentType(path)</code> function checks the file suffix to use the correct HTTP <code>Content-Type</code> for the response.</p>

<p>It will serve files from <code>http://&lt;server-name&gt;:8080/</code>.<br />
For the rest of the example, I will use the name of my machine, <code>blackbook.local</code>: to access the Web application from my iPhone, I am using <a href="http://blackbook.local:8080">http://blackbook.local:8080</a>.</p>

<p>In future iterations, I will add more interesting code to node.js (to use WebSockets for example) but for the moment this simple server is enough.</p>

<h1>Web Application</h1>

<p>Let&#8217;s now focus on the client-side part of the Web application.</p>

<h2>index.html</h2>

<p>The Web application is loaded from a single HTML5 page, <code>index.html</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;!doctype html&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;viewport&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width=device-width, user-scalable=0&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Board<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span>stylesheet <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span>screen.css&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
  <span style="color: #009900;">&lt;canvas <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;board&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;320&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;460&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>canvas&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'jquery.min.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>  
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;client.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: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>


<p>The page contains a single &lt;canvas> element named <code>board</code>. All the code to draw on the canvas is in <code>client.js</code> JavaScript file.</p>

<h2>client.js</h2>

<p><code>client.js</code> contains code to:</p>

<ul>
<li>get orientation information from the device (using the DeviceOrientation Event API)</li>
<li>draw the board and the piece based on the orientation information (using the Canvas API)</li>
</ul>

<p>First, it defines a few constants used to draw the board and the piece:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> kBoardWidth <span style="color: #339933;">=</span> <span style="color: #CC0000;">320</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> kBoardHeight <span style="color: #339933;">=</span> <span style="color: #CC0000;">460</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> kCircleRadius <span style="color: #339933;">=</span> <span style="color: #CC0000;">32</span><span style="color: #339933;">;</span></pre></div></div>


<p>The <code>piece</code> object is defined literally:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> piece <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
   center <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      x<span style="color: #339933;">:</span> kBoardWidth <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>
      y<span style="color: #339933;">:</span> kBoardHeight <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>
      xShift <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
      yShift <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
   color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#000&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>


<p>The piece has a center position with its coordinates (initially at the center of the board) and its acceleration on the x and y axes, and its color is black.</p>

<p>I use <a href="http://jquery.com/">jQuery</a> to bootstrap the application when the document is ready:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> board <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;board&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> board.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   window.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;devicemotion&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> accel <span style="color: #339933;">=</span> event.<span style="color: #660066;">accelerationIncludingGravity</span><span style="color: #339933;">;</span>
      piece.<span style="color: #660066;">center</span> <span style="color: #339933;">=</span> computeCenter<span style="color: #009900;">&#40;</span>piece.<span style="color: #660066;">center</span><span style="color: #339933;">,</span> accel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      drawGrid<span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      drawPiece<span style="color: #009900;">&#40;</span>context<span style="color: #339933;">,</span> piece<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #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: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>First we keep a reference on the <code>board</code> canvas that was declared in the HTML page and its associated 2D context.
The <code>context</code> will be used to draw the piece and the board on the canvas.</p>

<p>We use the DeviceOrientation API (supported by the iPhone and the iPad since iOS 4.2.1) to detect the acceleration of the device.
Periodically, the browser will call the handler associated to the <code>devicemotion</code> event.<br />
When that happens, the handler retrieves the <code>accelerationIncludingGravity</code> property, compute the new center position of the piece and draw the board and the piece on the canvas.</p>

<p>The method to draw the board is using the 2D context (the board is displayed as a grid):</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> drawBoard<span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   context.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> kBoardWidth<span style="color: #339933;">,</span> kBoardHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> kBoardWidth<span style="color: #339933;">;</span> x <span style="color: #339933;">+=</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> kBoardHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</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> y <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> kBoardHeight<span style="color: #339933;">;</span> y <span style="color: #339933;">+=</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      context.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      context.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>kBoardWidth<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   context.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#eee&quot;</span><span style="color: #339933;">;</span>
   context.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>The code to draw the piece is also using the 2D context to draw a circle from the piece&#8217;s center and its color:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> drawPiece<span style="color: #009900;">&#40;</span>context<span style="color: #339933;">,</span> piece<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   context.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> piece.<span style="color: #660066;">color</span><span style="color: #339933;">;</span>
   context.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   context.<span style="color: #660066;">arc</span><span style="color: #009900;">&#40;</span>piece.<span style="color: #660066;">center</span>.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> piece.<span style="color: #660066;">center</span>.<span style="color: #660066;">y</span><span style="color: #339933;">,</span> kCircleRadius<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   context.<span style="color: #660066;">closePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   context.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>Finally, we need to compute the updated position of the piece&#8217;s center based on its current position and the acceleration information from 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> computeCenter <span style="color: #009900;">&#40;</span>oldCenter<span style="color: #339933;">,</span> acceleration<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   newCenter <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
   newCenter.<span style="color: #660066;">xShift</span> <span style="color: #339933;">=</span> oldCenter.<span style="color: #660066;">xShift</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.8</span> <span style="color: #339933;">+</span> acceleration.<span style="color: #660066;">x</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2.0</span><span style="color: #339933;">;</span>
   newCenter.<span style="color: #660066;">yShift</span> <span style="color: #339933;">=</span> oldCenter.<span style="color: #660066;">yShift</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.8</span> <span style="color: #339933;">+</span> acceleration.<span style="color: #660066;">y</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2.0</span><span style="color: #339933;">;</span>
   newCenter.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> oldCenter.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> oldCenter.<span style="color: #660066;">xShift</span><span style="color: #339933;">;</span>
   <span style="color: #006600; font-style: italic;">// use *minus* to compute the center's new y</span>
   newCenter.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> oldCenter.<span style="color: #660066;">y</span> <span style="color: #339933;">-</span> oldCenter.<span style="color: #660066;">yShift</span><span style="color: #339933;">;</span>
   <span style="color: #006600; font-style: italic;">// do not go outside the boundaries of the canvas</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newCenter.<span style="color: #660066;">x</span> <span style="color: #339933;">&lt;</span> kCircleRadius<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      newCenter.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> kCircleRadius<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>newCenter.<span style="color: #660066;">x</span> <span style="color: #339933;">&gt;</span> kBoardWidth <span style="color: #339933;">-</span> kCircleRadius<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      newCenter.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> kBoardWidth <span style="color: #339933;">-</span> kCircleRadius<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>newCenter.<span style="color: #660066;">y</span> <span style="color: #339933;">&lt;</span> kCircleRadius<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      newCenter.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> kCircleRadius<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>newCenter.<span style="color: #660066;">y</span> <span style="color: #339933;">&gt;</span> kBoardHeight <span style="color: #339933;">-</span> kCircleRadius<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      newCenter.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> kBoardHeight <span style="color: #339933;">-</span> kCircleRadius<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000066; font-weight: bold;">return</span> newCenter<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>To smooth the movement and increase the acceleration when the mobile is moved, I applied a low-pass filter:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">newCenter.<span style="color: #660066;">xShift</span> <span style="color: #339933;">=</span> oldCenter.<span style="color: #660066;">xShift</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.8</span> <span style="color: #339933;">+</span> acceleration.<span style="color: #660066;">x</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2.0</span><span style="color: #339933;">;</span>
newCenter.<span style="color: #660066;">yShift</span> <span style="color: #339933;">=</span> oldCenter.<span style="color: #660066;">yShift</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.8</span> <span style="color: #339933;">+</span> acceleration.<span style="color: #660066;">y</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2.0</span><span style="color: #339933;">;</span></pre></div></div>


<p>The rest of the method ensures that the piece will remain in the boundaries of the board.</p>

<h1>screen.css</h1>

<p>Last thing is to make sure that the canvas fills the whole Web page by removing the margin and padding on the <code>body</code> element. This is done through CSS in the <code>screen.css</code> file:</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">body <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">margin</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>


<p>All these files are located in the same directory than <code>server.js</code>.</p>

<h1>Run the example</h1>

<p>Let&#8217;s start the node.js server:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ node server.js
HTTP server running at htpp:<span style="color: #000000; font-weight: bold;">//</span>0.0.0.0:<span style="color: #000000;">8080</span></pre></div></div>


<p>Now, from my iPhone, I go to the Web page on <a href="http://blackbook.local:8080">http://blackbook.local:8080</a> (if you go on the Web application from your machine, nothing will happen since the <code>devicemotion</code> event is not available on desktop browsers).</p>

<p>As I have not found a way to prevent the iPhone to change the screen orientation when it is moved, I have locked the iPhone in portrait mode before running the Web application.</p>

<p>Here is a small video of the result:</p>

<p><object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/hqVcLpsO728?fs=1&amp;hl=fr_FR&amp;rel=0&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/hqVcLpsO728?fs=1&amp;hl=fr_FR&amp;rel=0&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object></p>

<p>It&#8217;s not obvious from the video but the camera was recording from above: the iPhone was held horizontally.</p>

<h2>Source Code</h2>

<p>I have pushed the application on <a href="https://github.com/jmesnil/board-node">Github</a>. You can clone it with Git:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>jmesnil<span style="color: #000000; font-weight: bold;">/</span>board-node.git</pre></div></div>


<p>The code is slightly different as I have already started the next iteration but the main idea remains the same.</p>

<h2>Conclusion</h2>

<p>With a few lines of JavaScript on both the client and server sides, it is possible to:</p>

<ul>
<li>use node.js to write a Web server returning files</li>
<li>use the Canvas API to draw things on a Web page</li>
<li>use the DeviceOrientation Event API orientation to have interaction between the device and the Web page </li>
</ul>

<p>Next steps will be to improve the user experience:</p>

<ul>
<li>make the application appear as a standalone application on the iPhone home screen and remove Safari chrome</li>
<li>support offline mode to use the Web application even when the server is not running</li>
</ul>

<h1>Further Reading</h1>

<ul>
<li><a href="http://nodejs.org/">node.js</a></li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">Canvas API</a> (and its corresponding <a href="http://diveintohtml5.org/canvas.html">&#8220;Dive into HTML5&#8243; chapter</a>)</li>
<li><a href="http://dev.w3.org/geo/api/spec-source-orientation.html">DeviceOrientation Event API</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2010/11/24/html5-web-application-for-iphone-and-ipad-with-node-js/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#9873; Updated split bookmarklets</title>
		<link>http://jmesnil.net/weblog/2008/08/04/updated-split-bookmarklets/</link>
		<comments>http://jmesnil.net/weblog/2008/08/04/updated-split-bookmarklets/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 20:31:49 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jmesnil.net/weblog/?p=270</guid>
		<description><![CDATA[I was doing some house-cleaning on my weblog after upgrading WordPress and redesigning its presentation (in a minimalist but not so minimalist fashion) when I saw that my popular split bookmarklets were broken due to a bad formatting. I have fixed them and checked that they work correctly with Firefox 3, Safari 3 and WebKit [...]]]></description>
			<content:encoded><![CDATA[<p>I was doing some house-cleaning on my weblog after upgrading WordPress and redesigning its presentation (in a <a href="http://diveintomark.org/archives/2008/06/21/minimalism">minimalist</a> but <a href="http://tomayko.com/writings/administrative-debris">not so minimalist</a> fashion) when I saw that my popular <a href="http://jmesnil.net/weblog/2004/07/16/a-bookmarklet-to-split-html-document-within-the-browser/">split bookmarklets</a> were broken due to a bad formatting.</p>

<p><img src="http://jmesnil.net/weblog/wp-content/uploads/2008/08/split-bookmarklets.png" alt="" title="split-bookmarklets" width="499" height="433" class="aligncenter" /></p>

<p>I have fixed them and checked that they work correctly with Firefox 3, Safari 3 and WebKit nightly builds (my preferred browser these days) but I have not checked if they work on IE and Opera. If you can test on these browsers, let me know if they work.</p>
]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2008/08/04/updated-split-bookmarklets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#9873; Review of JavaScript: The Good Parts</title>
		<link>http://jmesnil.net/weblog/2008/05/27/review-of-javascript-the-good-parts/</link>
		<comments>http://jmesnil.net/weblog/2008/05/27/review-of-javascript-the-good-parts/#comments</comments>
		<pubDate>Tue, 27 May 2008 20:18:38 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[book]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://jmesnil.net/weblog/?p=178</guid>
		<description><![CDATA[I admit: I don&#8217;t like JavaScript. I have written and maintained very few scripts and it was a frustrating experience to have them working on several browsers. However this was many moons ago and I wanted to reevaluate JavaScript after using it in unexpected places (i.e. outside of the browser) like in CouchDB View API [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://oreilly.com/catalog/9780596517748/"><img src='http://oreilly.com/catalog/covers/9780596517748_cat.gif' class='alignright' /></a></p>

<p>I admit: I don&#8217;t like <a href="http://en.wikipedia.org/wiki/JavaScript">JavaScript</a>.</p>

<p>I have written and maintained very few scripts and it was a frustrating experience to have them working on several browsers.<br />
However this was many moons ago and I wanted to reevaluate JavaScript after using it in unexpected places (i.e. outside of the browser) like in <a href="http://wiki.apache.org/couchdb/HttpViewApi">CouchDB View API</a> or <a href="http://www.eclipse.org/dash/monkey-help.php?key=writing">Eclipse Monkey</a> (I already wrote about <a href="http://jmesnil.net/weblog/2007/05/23/jmx-scripts-with-eclipse-monkey/">JMX scripts using Eclipse Monkey</a>).</p>

<p>I was looking for a good book on <em>JavaScript, the language</em>.
There are many many books on JavaScript but they focus mainly on <em>JavaScript in the browser</em> and spend thousands pages describing the DOM (please, Messrs. the editors, save the Amazon forest and just print some links to the online DOM documentation&#8230;).<br />
I wanted a concise book about writing simple and maintainable code. I also wanted to learn more about the weird syntax constructions spotted when reading non-trivial bits of code, such as <a href="http://ejohn.org/blog/processingjs/">processing.js</a> or <a href="http://svn.apache.org/viewvc/incubator/couchdb/trunk/share/server/main.js?view=markup">CouchDB View&#8217;s map/reduce</a>.</p>

<p>I bought &#8220;<a href="http://oreilly.com/catalog/9780596517748/">JavaScript: The Good Parts</a>&#8221; on the strength of the <a href="http://www.crockford.com/">author</a>&#8216;s chapter in &#8220;<a href="http://www.oreilly.com/catalog/9780596510046/">Beautiful Code</a>&#8221; and I was not disappointed.<br />
This is exactly the kind of book I&#8217;m looking forward to when learning a programming language. It is short (100 pages + 50 pages of appendixes including <a href="http://json.org/">JSON</a> description) but dense, the sample code are small and meaningful (even the <em>done to death</em> <code>fibonacci</code> and <code>factorial</code> functions used here to explain <a href="http://en.wikipedia.org/wiki/Memoization">memoization</a>).<br />
The book does not lose space describing extensively the whole language. It focuses on the subset which is good and proven and do not talk about the edges or the parts which are better forgotten. It really shows how to write code which is both readable, maintainable <em>and</em> elegant.<br />
I&#8217;m sure I&#8217;ll come back to this book every time I read JavaScript code using some peculiarities of the language that I don&#8217;t understand.</p>

<p>After reading this book, I&#8217;ve got a better understanding of JavaScript and now sees the good (and even beautiful) parts of it.
I have a better appreciation for its prototype-based design even if it is hindered by a class-based syntax.<br />
I also find it frustrating that the JavaScript standard library is so useless (no I/O to communicate with the rest of the world). Of course, the library is richer when the code is to run in the browser or on Rhino (gaining access to the whole Java platform) but, by itself, the standard library is very poor compared to what comes bundled with <a href="http://python.org/">Python</a> or <a href="http://www.ruby-lang.org/fr/">Ruby</a>.</p>

<p>These are critics of the language. For the book, I&#8217;ve got nothing but praises. I recommend it to any programmer wanting to learn more about what is good in JavaScript.</p>

<p>One advice about the code examples: it is much simpler to use <a href="http://www.mozilla.org/js/spidermonkey/">Spidermonkey</a> or <a href="http://www.mozilla.org/rhino/">Rhino</a> than the web browser to run the different scripts and experiment with them interactively.</p>
]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2008/05/27/review-of-javascript-the-good-parts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#9873; How to include JavaScript engine in Apple&#8217;s Java 6 VM</title>
		<link>http://jmesnil.net/weblog/2008/05/14/how-to-include-javascript-engine-in-apples-java-6-vm/</link>
		<comments>http://jmesnil.net/weblog/2008/05/14/how-to-include-javascript-engine-in-apples-java-6-vm/#comments</comments>
		<pubDate>Wed, 14 May 2008 12:22:33 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[apple]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[macosx]]></category>

		<guid isPermaLink="false">http://jmesnil.net/weblog/?p=175</guid>
		<description><![CDATA[After complaining in my previous post, here is a more constructive guide to use JavaScript with Apple&#8217;s Java 6 VM: Download JSR 223&#8242;s engines Copy jsr223-engines/javascript/build/js-engine.jar to /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/ Download Rhino Copy rhino1_7R1/js.jar to /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/ You can now use a &#8220;JavaScript&#8221; engine from Apple&#8217;s Java 6 VM: public class JavaScriptTest &#123; public static void main&#40;String&#91;&#93; args&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>After complaining in my <a href="http://jmesnil.net/weblog/2008/05/14/no-javascript-in-java-6-on-mac-os-x/">previous post</a>, here is a more constructive guide to use JavaScript with Apple&#8217;s Java 6 VM:</p>

<ol>
<li>Download <a href="https://scripting.dev.java.net/files/documents/4957/37593/jsr223-engines.zip">JSR 223&#8242;s engines</a></li>
<li>Copy <code>jsr223-engines/javascript/build/js-engine.jar</code> to <code>/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/</code></li>
<li>Download <a href="http://www.mozilla.org/rhino/download.html">Rhino</a></li>
<li>Copy <code>rhino1_7R1/js.jar</code> to <code>/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/lib/ext/</code></li>
</ol>

<p>You can now use a &#8220;JavaScript&#8221; engine from Apple&#8217;s Java 6 VM:</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JavaScriptTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
        ScriptEngineManager factory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScriptEngineManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ScriptEngine engine <span style="color: #339933;">=</span> factory.<span style="color: #006633;">getEngineByName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;JavaScript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        engine.<span style="color: #006633;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;print('hello, world!')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p><code>jrunscript</code> is also working:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">    $ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>System<span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>Frameworks<span style="color: #000000; font-weight: bold;">/</span>JavaVM.framework<span style="color: #000000; font-weight: bold;">/</span>Versions<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.6</span><span style="color: #000000; font-weight: bold;">/</span>Home<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>
    $ .<span style="color: #000000; font-weight: bold;">/</span>jrunscript 
    rhino-nonjdk<span style="color: #000000; font-weight: bold;">&gt;</span> print<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;hello, world&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;
    hello, world
    rhino-nonjdk<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2008/05/14/how-to-include-javascript-engine-in-apples-java-6-vm/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#9873; No JavaScript in Java 6 on Mac OS X</title>
		<link>http://jmesnil.net/weblog/2008/05/14/no-javascript-in-java-6-on-mac-os-x/</link>
		<comments>http://jmesnil.net/weblog/2008/05/14/no-javascript-in-java-6-on-mac-os-x/#comments</comments>
		<pubDate>Wed, 14 May 2008 11:39:03 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[apple]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[macosx]]></category>

		<guid isPermaLink="false">http://jmesnil.net/weblog/?p=173</guid>
		<description><![CDATA[I had an idea about using JavaScript from Java 6 and I wanted to give it a try on my MacBook. No such luck: Apple has recently released Java 6 for Mac OS X Leopard but somehow it does not include Rhino, the Mozilla&#8217;s JavaScript engine bundled in Sun Java 6 release. Instead they provided [...]]]></description>
			<content:encoded><![CDATA[<p>I had an idea about using <a href="http://java.sun.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html">JavaScript from Java 6</a> and I wanted to give it a try on my MacBook.<br />
No such luck: Apple has recently released Java 6 for Mac OS X Leopard but somehow it does not include <a href="http://www.mozilla.org/rhino/">Rhino</a>, the Mozilla&#8217;s JavaScript engine bundled in <a href="http://java.sun.com">Sun</a> Java 6 release.</p>

<p>Instead they provided only one engine for <a href="http://www.apple.com/applescript/">AppleScript</a> but frankly:</p>

<p><img src="http://jmesnil.net/weblog/wp-content/uploads/2008/07/whocares1-300x122.png" alt="" title="whocares1-300x122" width="300" height="122" class="aligncenter size-full wp-image-186" /></p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AppleScriptTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
        ScriptEngineManager factory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScriptEngineManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ScriptEngine engine <span style="color: #339933;">=</span> factory.<span style="color: #006633;">getEngineByName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AppleScript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        engine.<span style="color: #006633;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;tell application <span style="color: #000099; font-weight: bold;">\&quot;</span>Finder<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\n</span> display dialog <span style="color: #000099; font-weight: bold;">\&quot;</span>Who cares?<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\n</span> end tell&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>Nuff said&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2008/05/14/no-javascript-in-java-6-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#9873; Updated Split bookmarklets (works on IE6)</title>
		<link>http://jmesnil.net/weblog/2004/07/25/updated-split-bookmarklets-works-on-ie6/</link>
		<comments>http://jmesnil.net/weblog/2004/07/25/updated-split-bookmarklets-works-on-ie6/#comments</comments>
		<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://jmesnil.net/blog/2004/07/25/updated-split-bookmarklets-works-on-ie6/</guid>
		<description><![CDATA[Thanks to the many comments I had about the split bookmarklets, I updated them and here they are: split horizontally in current window -&#62; drag to your bookmark folder split vertically in current window -&#62; drag to your bookmark folder the main changes are: they work on on Internet Explorer 6 (many thanks to this [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to the many comments I had about the <a href="http://www.jmesnil.net/weblog/2004/07/bookmarklet-to-split-html-document.html">split bookmarklets</a>, I updated them and here they are:</p>

<ul>
<li><a href="javascript:document.write('&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;FRAMESET ROWS=\'50%,*\'&gt;&lt;FRAME SRC=' + location.href + '&gt;&lt;FRAME SRC=' + location.href + '&gt;&lt;/FRAMESET&gt;&lt;/HTML&gt;');document.close();">split horizontally in current window</a> -&gt; drag to your bookmark folder</li>

<li><a href="javascript:document.write('&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;FRAMESET COLS=\'50%,*\'&gt;&lt;FRAME SRC=' + location.href + '&gt;&lt;FRAME SRC=' + location.href + '&gt;&lt;/FRAMESET&gt;&lt;/HTML&gt;');document.close();">split vertically in current window</a> -&gt; drag to your bookmark folder</li>
</ul>

<p>the main changes are:</p>

<ul>
<li>they work on on Internet Explorer 6 (many thanks to <a href="http://www.jmesnil.net/weblog/2004/07/bookmarklet-to-split-html-document.html#109047543233946689">this anonymous commenter</a>)</li>
<li>Firefox throbber is stopped once the frames loading is finished</li> 
<li>the scrollbars appear only in the frames and not in Firefox (on some sites, it still does not work&#8230;)</li>
</ul>

<p>I&#8217;m by no means a JavaScript wizard but I find quite amazing the difficulties to get such a simple javascript code to work on different browsers. With that little experience, I&#8217;m even more impressed by web applications using JavaScript like <a href="http://gmail.google.com">Gmail</a> (<a href="http://www.oddpost.com">Oddpost</a> is also worth to take a look but they made their job easier by only targeting Internet Explorer).</p>
]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2004/07/25/updated-split-bookmarklets-works-on-ie6/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#9873; A bookmarklet to split HTML document within the browser</title>
		<link>http://jmesnil.net/weblog/2004/07/16/a-bookmarklet-to-split-html-document-within-the-browser/</link>
		<comments>http://jmesnil.net/weblog/2004/07/16/a-bookmarklet-to-split-html-document-within-the-browser/#comments</comments>
		<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate>
		<dc:creator>Jeff Mesnil</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://jmesnil.net/blog/2004/07/16/a-bookmarklet-to-split-html-document-within-the-browser/</guid>
		<description><![CDATA[[update: the bookmarklets were broken for a long time but they now work again on Firefox 3 and Safari 3] In a comment on his blog, I proposed to Cedric a simple solution to spilt a HTML page in two within the browser. It’s just a one line JavaScript but it can be handy to [...]]]></description>
			<content:encoded><![CDATA[<p>[<strong>update</strong>: the bookmarklets were broken for a long time but they now work again on Firefox 3 and Safari 3]</p>

<p>In a comment on his <a href="http://beust.com/weblog/archives/000153.html">blog</a>, I proposed to Cedric a simple solution to spilt a HTML page in two within the browser.
It’s just a one line JavaScript but it can be handy to use it,</p>

<p><a href="javascript:w=window.open('');w.document.write('&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;FRAMESET ROWS=\'50%,*\'&gt;&lt;FRAME SRC=' + location.href + '&gt;&lt;FRAME SRC=' + location.href + '&gt;&#038;lt/FRAMESET&gt;&lt;/HTML&gt;');w.document.close();">split document in new window</a> -&gt; drag to your bookmark folder</p></p>

<p>When you want to split a html document, click on the bookmark and you&#8217;re done (it works on Safari and Firefox) </p>

<p>Cedric then suggested to split the document in the current window. So here come two new bookmarklets:</p>

<ul>
<li><a href="javascript:document.write('&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;FRAMESET ROWS=\'50%,*\'&gt;&lt;FRAME SRC=' + location.href + '&gt;&lt;FRAME SRC=' + location.href + '&gt;&lt;/FRAMESET&gt;&lt;/HTML&gt;');document.close();">split horizontally in current window</a> -&gt; drag to your bookmark folder</li>

<li><a href="javascript:document.write('&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;FRAMESET COLS=\'50%,*\'&gt;&lt;FRAME SRC=' + location.href + '&gt;&lt;FRAME SRC=' + location.href + '&gt;&lt;/FRAMESET&gt;&lt;/HTML&gt;');document.close();">split vertically in current window</a> -&gt; drag to your bookmark folder</li>


</ul>
]]></content:encoded>
			<wfw:commentRss>http://jmesnil.net/weblog/2004/07/16/a-bookmarklet-to-split-html-document-within-the-browser/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

