How to do it...

  1. To test if localStorage or sessionStorage is available on our browser, we can use the following code:
(function() { 
   if (typeof sessionStorage != 'undefined') 
   // or 
   if (window['localStorage'] !== null) 
   // or using the ModernizR library 
   if (Modernizr.localstorage) 
    
})(); 
  1. Let's use the getItem(key) method:
var color = localStorage.getItem('white'); 
var color = localStorage['white']; 
var color = localStorage.white; 
  1. Now let's use the setItem(key, value) method to store data in the localStorage object:
localStorage.setItem('name', $('#name').val()); 
localStorage['name'] = $('#name').val(); 
localStorage.name = $('#name').val(); 
  1. Next, we will store a complex object in the localStorage:
var colors = { white: '#000', black: ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.