HTML5 localstorage

I have spend some time experimenting with the new HTML5 localStorage feature. It is a new feature that makes it easy to store data in the web browser, using JavaScript.

Local storage does NOT replace a real database, since it is unique for the browser and cannot hold that large amounts of data,  but it is a really great tool for many smaller tasks.

For instance, say that you have an iPhone web application that needs to uniquely identify the mobile device. The device itself does not send any such information (like the MAC address, phone number etc.) so, you’re at a loss…

…but with localStorage, you can easily create a unique ID (e.g. a GUID) and store it in the local storage. The browser can then use that ID to identify itself to the system.

For instance, this is all you need to store a small string into the localStorage:

function hasLocalStorage() {
   return !typeof(localStorage) == "undefined";
}
localStorage.setItem("name", "Daniel");
var name = localStorage.getItem("name");
localStorage.removeItem("name");

As you can see, it’s really easy and really powerful.

Advertisements