HTML5 Local Storage

HTML5 Local Storage

Do you know about HTML Local Storage?

With local storage, web applications can store data locally within the user's browser. Before HTML5, application data had to be stored in cookies, included in every server request. Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.

Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data.

HTML Local Storage Objects
HTML local storage provides two objects for storing data on the client:

window.localStorage - stores data with no expiration date
window.sessionStorage - stores data for one session (data is lost when the browser tab is closed)

if(typeof(Storage) !== "undefined") {
// Code for localStorage/sessionStorage.
} else {
// Sorry! No Web Storage support..

The localStorage Object
The localStorage object stores the data with no expiration date. The data will not be deleted when the browser is closed, and will be available the next day, week, or year.

// How to Store
localStorage.setItem("lastname", "Victor");
// how to Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");

Example explained:

Create a localStorage name/value pair with name="lastname" and value="Victor"
Retrieve the value of "lastname" and insert it into the element with id="result"
The example above could also be written like this:

localStorage.lastname = "Victor";
document.getElementById("result").innerHTML = localStorage.lastname;

The syntax for removing the "lastname" localStorage item is as follows:

Note: Name/value pairs are always stored as strings. Remember to convert them to another format when needed!

The following example counts the number of times a user has clicked a button. In this code the value string is converted to a number to be able to increase the counter:


if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
document.getElementById("result").innerHTML = "You have clicked button " +
localStorage.clickcount + " time(s).";

The sessionStorage Object
The sessionStorage object is equal to the localStorage object, except that it stores the data for only one session. The data is deleted when the user closes the specific browser tab.

The following example counts the number of times a user has clicked a button, in the current session:

if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
sessionStorage.clickcount = 1;
document.getElementById("result").innerHTML = "You have clicked that button " +
sessionStorage.clickcount + " time(s) in this session.";

Before using local storage, check browser support for localStorage and sessionStorage:

Chrome       ---> 4.0 and above

IE               ----> 8.0 and above

Firefox      ----> 3.5 and above

Safari        -----> 4.0 and above

No Comments Yet.

Leave a comment