HTML5 Local Storage

Local Storage:

  • simply provides a key-value mapping. Present implementations only support string-to-string mappings, so you need to serialize and de-serialize other data structures.
  • Local storage limits to 5MB of usage only
  • By exceeding quota it will return "QUOTA_EXCEEDED_ERR"
HTML5 Local Storage

Imp things:
length,key,getItem,setItem,removeItem,clear

To set the item in local storage
localstorage.setItem(key, value);
ex: localstorage.setItem("uname", 'kongaraju'); //represents localStorage.key(0)

To store Objects in local storage
localStorage.setItem("Uinfo", JSON.stringify({ 'Fname' : 'Raju' , 'Lname' : 'konga' })); //key index 1

To get the item from LocalStorage
localstorage.getItem(key); // By Key
localStorage.key(index);     //BY Index
ex: var UserName=localstorage.getItem("uname");
        var UserName=localStorage.getItem(localStorage.key(index));
//return "kongaraju"

To get the objects from local storage
var info=JSON.parse(localSorage.getItem('Uinfo'));
var FirstName= info.Fname
var LastName= info.Lname

To get total length of local storage
localStorage.length

To get list of local Storage items
for(var i=o; i<localstorage.length; i++)
{
$(list).append(localStorage.getItem(localStorage.key(i)));
}

remove the item
localStorage.removeItem(key);
ex: localStorage.removeItem('uname');

To Clear the localstorage
localStorage.clear(); // it clears total localstorage of app

In other way
localStorage["uname"]='kongraju'; 
var UserName = localstorage['uname'];


Session Storage:

  • 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.

Share this

Related Posts

Previous
Next Post »