<?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>bemoko&#039;s blog on mobile &#187; localStorage</title>
	<atom:link href="http://blog.bemoko.com/tag/localstorage/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bemoko.com</link>
	<description>mobile made simple</description>
	<lastBuildDate>Thu, 19 Aug 2010 13:58:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cool &#8230; iPhone handles the HTML5 onstorage event handler</title>
		<link>http://blog.bemoko.com/2009/09/16/cool-iphone-handles-the-html5-onstorage-event-handler/</link>
		<comments>http://blog.bemoko.com/2009/09/16/cool-iphone-handles-the-html5-onstorage-event-handler/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 17:08:05 +0000</pubDate>
		<dc:creator>Ian Homer</dc:creator>
				<category><![CDATA[mobile]]></category>
		<category><![CDATA[mobile web]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[localStorage]]></category>
		<category><![CDATA[offline]]></category>
		<category><![CDATA[onstorage]]></category>

		<guid isPermaLink="false">http://blog.bemoko.com/?p=651</guid>
		<description><![CDATA[&#8230; or in other words we can readily synchronise local browser storage with back-send server persistent storage.  This is one of the fundamentals of compelling off line web applications.
So a little background.  You may know that you can store data in your local browser database with javascript like &#8230;

1
2
3
4
5
6
7
8
9
10
11
12
13
function store&#40;&#41; &#123;
  if [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230; or in other words we can readily synchronise local browser storage with back-send server persistent storage.  This is one of the fundamentals of compelling off line web applications.</p>
<p>So a little background.  You may know that you can store data in your local browser database with javascript like &#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> store<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">localStorage</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> count<span style="color: #339933;">=</span>window.<span style="color: #660066;">localStorage</span>.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;count&quot;</span><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><span style="color: #339933;">!</span>count<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      count<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    count<span style="color: #339933;">++</span>
    window.<span style="color: #660066;">localStorage</span>.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;count&quot;</span><span style="color: #339933;">,</span>count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  store<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>and you can even see it in action <a href="http://localhost:8080/live/test/i/suite/javascript-offline/test/store">here</a>, if you got a decent browser such as the latest iPhone 3, Firefox 3.5, Safari 4.</p>
<p>But this ain&#8217;t much good if you can&#8217;t get this data back to the server to do something useful with it, e.g. share with friends, share with your other devices, keep a backup, send a message &#8230; I could go on.</p>
<p>So what we really need is a way to easily listen out to storage events and deal with it in one place.  Yep, we could create our own Javascript framework to do this and handle getters and setters, but that sounds nasty to me.</p>
<p>Instead we can know use the <a href="http://dev.w3.org/html5/spec/Overview.html#handler-window-onstorage">onstorage</a> attribute on the HTML body tag to hook into a function that will handle all of these call backs based on local stored data.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> store<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">localStorage</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> count<span style="color: #339933;">=</span>window.<span style="color: #660066;">localStorage</span>.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;count&quot;</span><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><span style="color: #339933;">!</span>count<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      count<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    count<span style="color: #339933;">++</span>
    window.<span style="color: #660066;">localStorage</span>.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;count&quot;</span><span style="color: #339933;">,</span>count<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: #003366; font-weight: bold;">function</span> handleOnStorage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  myFunctionToSendDataToServer<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">key</span><span style="color: #339933;">,</span>
    event.<span style="color: #660066;">newValue</span><span style="color: #339933;">,</span> event.<span style="color: #660066;">oldValue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  document.<span style="color: #660066;">body</span>.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;onstorage&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;handleOnStorage();&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  store<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Take a look at it in action <a href="http://test.bemoko.com/test/i/suite/javascript-offline/test/onstorage">here</a> in the bemoko mobile test suite.   It works on iPhone 3 and Safari 4.  You can even see the complete code <a href="http://test.bemoko.com/test/i/suite/javascript-offline/test/onstorage/source/true">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bemoko.com/2009/09/16/cool-iphone-handles-the-html5-onstorage-event-handler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
