What is HTML5 Web Storage?

How to use web storage in html5

With HTML5, web pages can store data locally within the user’s browser.

HTML5 Web Storage provides two new objects for storing data on the client:

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

Before using web storage, check browser support for localStorage and sessionStorage:
if(typeof(Storage) !== “undefined”) {
// Code for localStorage/sessionStorage.
} else {
// commenting ! there is no support for web support. update your browser
}

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.
Example
// Store
localStorage.setItem(“lastname”, “josph”);
// Retrieve
document.getElementById(“result”).innerHTML = localStorage.getItem(“lastname”);
Exlanation:

It would generate a localStorage name/value pair with name=”lastname” and value=”josph”
Retrieve the value of “lastname” and insert it into the element with id=”result”

The example above could also be written like this:
// Store
localStorage.lastname = “josph”;
// Retrieve
document.getElementById(“result”).innerHTML = localStorage.lastname;

The syntax for removing the “lastname” localStorage item is as follows:
localStorage.removeItem(“lastname”);
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:

How to use web storage in html5
Example
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById(“result”).innerHTML = “You have clicked the button ” +
localStorage.clickcount + ” time(s).”;

how ot use 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 browser window.

The following example counts the number of times a user has clicked a button, in the current session:
Example
if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
sessionStorage.clickcount = 1;
}
document.getElementById(“result”).innerHTML = “You have clicked the button ” +
sessionStorage.clickcount + ” time(s) in this session.”;

Source

HTML5 TUTORIAL