Quickly Build HTML5 and PhoneGap Mobile Apps

Getting Started with HTML5 Local Storage

Posted: September 30th, 2011 | Author: | Filed under: Features, HTML5, JavaScript, Tutorials | Tags: , , | 9 Comments »

HTML5′s local storage is undoubtedly one of the most interesting and most talked about features in the HTML5 technology stack. Local storage is part of Web Storage specification and is supported by all modern browsers (destkop and mobile). Although local storage (or Web Storage) sounds rather sophisticated, the functionality is very easy to use. You basically get a map as storage inside the browser (available to all browser windows). You can insert, delete or read key/value pairs. That’s it. Data stored in local storage (localStorage) will be there when you close and open the browser. There is also session storage (sessionStorage). As the name implies, it will be only available as long as the browser window is open, and will be cleared when browser window is closed.

The only other thing to know is that data saved by a page is only available for a pages from the same domain. In other words, a page loaded from abc.com, doesn’t have access to data saved by page from domain xyz.com.

We are going to going to build an app that looks like the screen shot below. In fact, you can try the app here (click the image, or scan the QR code). Try it on your mobile device as well.

To build the app, I used Tiggr Mobile Apps Builder. If you are wondering why Tiggr? Well, because it’s incredibly simple and fast to create a project and build app. If you don’t have an account yet, quickly sign up here.

First build the UI by dragging and dropping jQuery Mobile components from the palette on to the phone. At any point, you can click Test to try the app in browser, or mobile browser.

You can use Tiggr to build real mobile apps without writing any JavaScript. But, for more advanced cases (like ours), you can easily write any custom JavaScript. You can even import 3rd party JavaScript libraries. In our case, we are going to create a new JavaScript file (called asset) with the following content:

// save item
function save(item) {
  var size = localStorage.length + 1;
  localStorage.setItem('key' + size, item);
}
// get storage content
function storage(){
   var output='';
   for (i=0; i <= localStorage.length - 1; i++)  {  
      key = localStorage.key(i);  
      val = localStorage.getItem(key);  
      if (i == 0) {
         output = val;
      }
      else {
         output = output + '\n' +val;
      }
   }
   return output;
}
// clear storage
function clear () {
   localStorage.clear();
}

There are three functions, one for saving a new item (save()), one for getting the current storage (storage()) and one for clearing the content (clear()). Local storage API is very simple. For example, to save an item:

localStorage.setItem('key', 'item');

then, to get a value from storage:

localStorage.getItem('key');

This is how the complete file looks inside Tiggr’s JavaScript editor:

The last step is to invoke JavaScript when the buttons are clicked. We also want to load storage content when the screen is loaded for the first time. Let’s work on the buttons first. To invoke JavaScript on button click, we first add click HTML event to the button:

Then we add Run Custom JavaScript action by clicking the + button:

Click on the action to enter JavaScript code. The code for Save to Local Storage button looks like this:

var item =$('[dsid="input"]').val();
save(item);
var output='';
output = storage();
$('[dsid="storageContent"]').text(output);

We first find the input component using jQuery (it’s going to simpler to do that, once we introduce Tiggr JavaScript API, work in progress). Save the value from the input element, reload storage content so we can display it inside the textarea.

Clear Local Storage button looks like this:

clear();
$('[dsid="storageContent"]').text('');
alert('Local storage cleared.');

Lastly, we also add load event to the screen itself so that we can show storage content when the screen loads for the first time:

var output='';
output = storage();
$('[dsid="storageContent"]').text(output);

Try it yourself (it’s easy and fun!) and try the finished app here.


9 Comments on “Getting Started with HTML5 Local Storage”

  1. 1 David Frahm said at 14:19 on September 30th, 2011:

    I don’t see a “Run Custom JavaScript” option for my button’s click event.

    Any ideas what I might be doing wrong?

  2. 2 max said at 14:21 on September 30th, 2011:

    @David: you are probably using Prototypes Builder, and not the App Builder. What’s the last action that you have in the list, is it ‘Close Popup’?

  3. 3 David Frahm said at 05:43 on October 1st, 2011:

    @Max: Yes, close popup. I logged in at the App Builder page (https://gotiggr.com/login/login.seam), but I guess I’m only seeing Prototypes Builder. Oh, wait… I see I only have a PB account. That explains it.

  4. 4 max said at 09:47 on October 1st, 2011:

    @David: If you go back to My Account, you should see Tiggr Mobile App Builder box - you should be able to start your trial account. Let me know when you are up and running.

  5. 5 David Frahm said at 18:55 on October 6th, 2011:

    Just noticed your comment. I started a trial account the other day and everything worked just as the tutorial outlined. Thanks!

  6. 6 roberto said at 13:26 on November 29th, 2011:

    hello! nice tut! i just have a question about getting the data from local storage. How can i use the data stored to generate a list? like this:
    - i have a form with some text input controls
    - i hit SAVE and the data from the form gets stored somewhere
    - then i go to a screen where i can see those data displayed in a list control
    - the label of the item are generated from the first text box of the form

    thats the idea. I’m trying to do a contacts app by the way, thats how i need it to work, but i’m just starting with this tool, and i’m still very confuse with it.
    Thanks!!

  7. 7 max said at 15:29 on November 29th, 2011:

    @roberto: hope this helps: http://blog.gotiggr.com/2011/11/new-tiggr-component-contacts/

  8. 8 roberto said at 06:15 on November 30th, 2011:

    do you guys know how to delcare global variables using tiggr? because i want to use some data arround all the proyect. but it seems like when i change screens the variables don’t go along with the flow.

  9. 9 max said at 15:44 on November 30th, 2011:

    @roberto: create a variable outside a function in a JavaScript file (asset):

    var someName;

    or like this:

    someName;

    it will be available on each screen.


Leave a Reply


  • − three = 5