Advertisement
eimkasp

Save Data To Local Storage

Jul 20th, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function saveDataToDomStorage(name, data)
  2. {
  3.     var jsonData;
  4.  
  5.     /*
  6.      * DOM storage can't store complex data (object, array...)
  7.      * We have to serialize the data in a simple format, JSON.
  8.      *
  9.      * We get a string standing for the complex object,
  10.      * and store this string in the DOM storage.
  11.      *
  12.      * Complex data -> JSON stringify -> Simple data (string)
  13.      */
  14.     jsonData = JSON.stringify(data);
  15.  
  16.     window.localStorage.setItem(name, jsonData);
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement