Using localStorage/sessionStorage in PhoneGap application

The HTML 5 specification introduced 2 objects that can be used for storing data in key-value format on the client side, i wanted to try this feature out so i did create this simple Todo list web page that can be used to create a TO DO list. This is how my Todo list application looks like
This is the HTML page that i created for working with

 <!DOCTYPE html>
<html manifest="storage.appcache">
    <title>Offline Application Example</title>
    <script type="text/javascript" charset="utf-8" 
    <script type="text/javascript" charset="utf-8">
    var storage = window.localStorage;
      console.log('Inside document.ready');
        console.log('Entering clearstorage');
        console.log('Exiting clearstorage');
    function remove_item(key){
      console.log('Entering remove_item');
      console.log('Find and remove element with id = ' + key)
      console.log('Exiting remove_item');
    function add_item() {
      console.log('Entering add_item');
      var d = new Date();
      var key = d.getTime();
      var value = $('#new_item').val();
      console.log('Exiting add_item');
    function initTodoList(){
      console.log("Entering initTodoList " + storage.length);
      for(var i = 0; i < storage.length; i++){
        var key = storage.key(i);
        var value = storage.getItem(key);
    function createToDoListItem(key, value){
      var html = '<li  data-key="'+key+'" id="'+key+'">'
      +value+'<button onclick="javascript:remove_item(\''+ key+ '\')" 
      console.log('Appending html ' + html)
    <input type="text" id="new_item">
  <button onclick="add_item()">
  <ul id="todo_list">
  <button id="clearStorage">Clear storage</button>
You can use either localStorage, in which case the data is storage across browser restarts or you can use the sessionStorage in which case you will loose the TODO list once the browser is closed. All you have to do is change the value of var storage = either window.localStorage or window.sessionStorage Chrome developer tools allows us to look at the data stored in the localStorage as well as remove that data.

No comments: