<?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; javascript</title>
	<atom:link href="http://blog.bemoko.com/tag/javascript/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>
		<item>
		<title>iPhone 3.0 geolocation javascript API</title>
		<link>http://blog.bemoko.com/2009/06/17/iphone-30-geolocation-javascript-api/</link>
		<comments>http://blog.bemoko.com/2009/06/17/iphone-30-geolocation-javascript-api/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 21:04:42 +0000</pubDate>
		<dc:creator>Ian Walsh</dc:creator>
				<category><![CDATA[mobile web]]></category>
		<category><![CDATA[geolocation]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone 3.0]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[LBS]]></category>
		<category><![CDATA[safari]]></category>

		<guid isPermaLink="false">http://blog.bemoko.com/?p=415</guid>
		<description><![CDATA[With the iPhone 3.0 firmware released today, I thought I&#8217;d show you how to access one of the features I&#8217;ve been most looking forward to &#8211; support for the geolocation API in Safari means I can now create location aware websites.
This will be just a quick demo to retrieve and display the current longitue and [...]]]></description>
			<content:encoded><![CDATA[<p>With the iPhone 3.0 firmware released <a title="today" href="http://www.apple.com/uk/iphone/softwareupdate/">today</a>, I thought I&#8217;d show you how to access one of the features I&#8217;ve been most looking forward to &#8211; support for the <a title="geolocation" href="http://dev.w3.org/geo/api/spec-source.html">geolocation API</a> in Safari means I can now create location aware websites.</p>
<p>This will be just a quick demo to retrieve and display the current longitue and latitude of your phone, along with a googlemap so we can see if the results are right.  The more interesting applications will come&#8230;.</p>
<p>To do this we&#8217;ll use javascript and the newly added <a title="Navigator.Geolocation" href="http://dev.w3.org/geo/api/spec-source.html#geolocation">Navigator.Geolocation</a> interface to call the <a title="getCurrentPosition" href="http://dev.w3.org/geo/api/spec-source.html#get-current-position">getCurrentPosition</a>() function to retrieve the current longitude and latitude of the phone, which we&#8217;ll display and pass to the googlemaps API.</p>
<p>The code:</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
19
20
21
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;UTF-8&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE html <span style="color: #003366; font-weight: bold;">PUBLIC</span> <span style="color: #3366CC;">&quot;-//WAPFORUM//DTD XHTML Mobile 1.0//EN&quot;</span> <span style="color: #3366CC;">&quot;http://www.wapforum.org/DTD/xhtml-mobile10.dtd&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>html xmlns<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>iPhone <span style="color: #CC0000;">3.0</span> geolocation demo<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>meta content<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;&quot;</span> <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;viewport&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">function</span> handler<span style="color: #009900;">&#40;</span>location<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> message <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
message.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;&lt;img src='http://maps.google.com/staticmap?center=&quot;</span> <span style="color: #339933;">+</span> location.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> location.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&amp;size=300x200&amp;maptype=hybrid&amp;zoom=16&amp;key=YOURGOOGLEAPIKEY' /&gt;&quot;</span><span style="color: #339933;">;</span>
message.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">+=</span><span style="color: #3366CC;">&quot;&lt;p&gt;Longitude: &quot;</span> <span style="color: #339933;">+</span> location.<span style="color: #660066;">coords</span>.<span style="color: #660066;">longitude</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
message.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">+=</span><span style="color: #3366CC;">&quot;&lt;p&gt;Latitude: &quot;</span> <span style="color: #339933;">+</span> location.<span style="color: #660066;">coords</span>.<span style="color: #660066;">latitude</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
message.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">+=</span><span style="color: #3366CC;">&quot;&lt;p&gt;Accuracy: &quot;</span> <span style="color: #339933;">+</span> location.<span style="color: #660066;">coords</span>.<span style="color: #660066;">accuracy</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
navigator.<span style="color: #660066;">geolocation</span>.<span style="color: #660066;">getCurrentPosition</span><span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;message&quot;</span><span style="color: #339933;">&gt;</span>Location unknown<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>The results:</p>

<a href='http://blog.bemoko.com/2009/06/17/iphone-30-geolocation-javascript-api/iphone-geo-2211/' title='iPhone OS 2.2.1'><img width="150" height="150" src="http://blog.bemoko.com/wp-content/uploads/2009/06/iphone-geo-2211-150x150.jpg" class="attachment-thumbnail" alt="iPhone OS 2.2.1" title="iPhone OS 2.2.1" /></a>
<a href='http://blog.bemoko.com/2009/06/17/iphone-30-geolocation-javascript-api/iphone-geo-300-1/' title='iPhone 3.0'><img width="150" height="150" src="http://blog.bemoko.com/wp-content/uploads/2009/06/iphone-geo-300-1-150x150.jpg" class="attachment-thumbnail" alt="iPhone 3.0" title="iPhone 3.0" /></a>
<a href='http://blog.bemoko.com/2009/06/17/iphone-30-geolocation-javascript-api/iphone-geo-300-2/' title='iPhone 3.0 - I am here!'><img width="150" height="150" src="http://blog.bemoko.com/wp-content/uploads/2009/06/iphone-geo-300-2-150x150.jpg" class="attachment-thumbnail" alt="iPhone 3.0 - I am here!" title="iPhone 3.0 - I am here!" /></a>

<p>Try it for yourself by pointing your Safari browser at <a href="http://bemoko.com/blog/iphonegeo">http://bemoko.com/blog/iphonegeo</a>.</p>
<p>Update 27/7/2009: I noticed the zoom level on the google static map <a href="http://code.google.com/apis/maps/documentation/staticmaps/">API</a> wasn&#8217;t being set so the map was zoomed all the way out. I&#8217;ve added a default zoom for the demo code above. Not sure if thats a new requirement from Google&#8217;s side?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bemoko.com/2009/06/17/iphone-30-geolocation-javascript-api/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
