tags:

views:

956

answers:

3

I'm taking my first step of a thousand miles with the new local storage and session storage found in html5.

http://www.w3.org/TR/offline-webapps/

Q: Is there a code example of using either session storage or local storage, where the user enters a value, the value is saved locally, the user then connects to the Internet on his 56K modem and the local storage is synced with a server?

+1  A: 

Easiest way would be to store it in flavor of a JSON string in a HTML 'data' attribute and and make use of setInterval() in Javascript to try sending the data every minute or ten (and ignoring the exception). In the server side, just use any JSON decoder to your taste (json_decode() in PHP, Google Gson in Java, etc) to decode the JSON into an understandable format of the server side language in question, so that it can process/store it further.

BalusC
+3  A: 

Instead of using the setInterval and blindly trying to send data to your server, check the navigator.onLine property:

if (navigator.onLine) {
   // Send data using XMLHttpRequest
} else {
   // Queue data locally to send later
}

You can also add listeners to the Window object for the "online" and "offline" events which will let you know when the browser has internet connectivity again.

Arne Roomann-Kurrik
Thanks Arne! I'm looking for a code example, so your answer is a good first step.
cf_PhillipSenn
+1 nice one. Is DOM0 but not standard.. How is browser support here? Doc here btw: https://developer.mozilla.org/En/DOM/Window.navigator.onLine
BalusC
Tested navigator.onLine successfully with Firefox 3.6, Safari 4, IE8, and Chrome 5 (dev) and I hear it's in Opera too.
Arne Roomann-Kurrik
+2  A: 

you can find an example on this page on hacks.mozilla.org

futtta
Thanks futtta. This will get me to take another step.
cf_PhillipSenn