HTML5 Local Storage

HTML5 Local Storage Usage In Your Web Applications

HTML5 local storage can be used in applications that need to save user data and preferences across application restarts.

In this article we would be talking about the methods using the local storage to increase the functionality of a web application.

Overview

HTML5 local storage is used for storing key value pairs on the client side. These key value pairs can be retrieved in HTML pages originating from the same domain. The Local storage data is stored on the disk and persists across application restarts

The Local Storage can be used across applications like gaming, where you save the progress of a game or a store high scores that can be later retrieved OR in Media applications, where an application embeds an audio or video stream, the app can store the timestamp of the audio/video streams in local storage.Since this data is stored across application restarts, you can start the audio/video stream from the last paused location.

JavaScript APIs

The basic methods ( JavaScript* APIs) for setting and getting item to/from storage are:


// store item
  localStorage.setItem("item_key", "value");

// retrieve item
  var data = localStorage.getItem("item_key");
 

Like other JavaScript objects, you can treat the LocalStorage object as an associative array. Instead of using the getItem() and setItem() methods, you can simply use square brackets.

For example, the above snippet of code can also be written as,


  // store item
  localStorage["item_key"]= value;

  // retrieve item
  var data = localStorage["item_key"];
  

Below is the code snippet (from Hangonman game ) which stores and retrieves the game state,


function saveGameState ()
{
    localStorage["com.intel.hom.wrongGuesses"] = wrongGuesses;
    localStorage["com.intel.hom.rightGuesses"] = rightGuesses;
    localStorage["com.intel.hom.word"] = word;
    localStorage["com.intel.hom.gameType"] = gameType;
    localStorage["com.intel.hom.gameInProgress"] = gameInProgress;
}

function restoreGameState ()
{
    if (localStorage && localStorage["com.intel.hom.word"] &&
	localStorage["com.intel.hom.gameInProgress"] && (localStorage["com.intel.hom.gameInProgress"] === "true"))
    {
	wrongGuesses = localStorage["com.intel.hom.wrongGuesses"] || "";
	rightGuesses = localStorage["com.intel.hom.rightGuesses"] || "";
	word = localStorage["com.intel.hom.word"];
	gameType = localStorage["com.intel.hom.gameType"] || 0;
	gameInProgress = true;
    }
    else {
	initGameState();
    }
} 

Unfortunately, present implementations only support string-to-string mappings, so you need to do marshalling to and from strings for other data structures such as an array or JavaScript object.

You can do so by using JSON.stringify() and JSON.parse() methods.


var ArrayData = [5, 6, 9];
// store array to localstorage
localStorage.setItem("list_data_key",  JSON.stringify(ArrayData));

// retrieve stored data (JSON stringified) and convert
var storedData = localStorage.getItem("ArrayData ");
if (storedData) {
    ArrayData = JSON.parse(storedData);
} 

Here’s an example restoring the settings, from Hangonman game:


function restoreSettings ()
{
    try {
        useSounds = JSON.parse(localStorage["com.intel.hom.useSounds"]);
    }
    catch (e) {
        useSounds = true;
        localStorage["com.intel.hom.useSounds"] = JSON.stringify(useSounds);
    }
}  

Other methods that you will probably use are, removeItem and clear as below,


// For removing single Key
localStorage.removeItem("item_key"); // where 'item_key' is the key of the value you want to remove

//  To clear all Local Storage
localStorage.clear();
 

For more information on the HTML5 storage APIs, refer to the following resources: