Storage Classes in HTML5

Storage in HTML was done with the help of cookies. The exciting thing about this storage is that it's fast as well as secure.

Two types of storage are in HTML5

  1. Local Storage : In this object stores data for a larger period of time even if the browser is closed.
  2. Session Storage : In this object stores data for a specific session.

Methods

Using local storage in modern browsers is ridiculously easy. All you have to do is modify the localStorage object in JavaScript. You can do that directly use the setItem() and getItem() method:

localStorage.setItem('favoriteflavor','vanilla');

If you read out the favoriteflavor key, you will get back "vanilla":

var taste = localStorage.getItem('favoriteflavor');
// -> "vanilla"

To remove the item, you can use- the removeItem() method:

localStorage.removeItem('favoriteflavor');
var taste = localStorage.getItem('favoriteflavor');
// -> null

That's it! You can also use sessionStorage instead of localStorage if you want the data to be maintained only until the browser window closes.

Ebook Download
View all
Learn
View all
Foreantech - A complete online solution company.