Practice Set - localStorage
by Sub Taper
HTML
<p>There is one part to this practice problem. This exercise is designed to get you comfortable with using <code>window.localStorage</code>, the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Storage" target="_blank">Storage object</a>, and the utilities of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify" target="_blank">JSON object</a>.</p>
<ol>
<li>On lines 11-12, the <code>personInfo</code> object is being converted into a string using JSON.stringify(), and assigned to localStorage. Your task is to do the opposite: retrieve the string from localStorage (Storage.getItem() may help), and output it to the page using the logMessage() function that's provided.
</li>
<p><b>Output:</b>
</p>
<div id="output"></div>
CSS
#output {
border:1px solid black;
padding: .5em;
}
JavaScript
// Initialize our personInfo Object
var personInfo = {
fname: "Bill",
lname: "Adama",
addr: "Galactica CIC",
email: "N/A",
title: "Admiral"
}
// Convert the object to a string for local storage
var jsonPerson = JSON.stringify(personInfo);
// Stores the string in local storage
window.localStorage.setItem("person", jsonPerson);
// Get the string back out of local storage
var cValue = window.localStorage.getItem("person", jsonPerson);
//
var backToObj = JSON.parse(cValue);
// create an object which will dump out all the properties of the object
function dumpObject(backToObj) {
for (var key in backToObj) {
// test to make sure its an object w properties
if (backToObj.hasOwnProperty(key)) {
logMessage( backToObj[key]);
}
}
}
dumpObject(backToObj);
// Utility function for logging convenience
// Logs msg to the element with given id
// If id is undefined, logs to #output
function logMessage(msg, id) {
if (!id) {
id = "output";
}
document.getElementById(id).innerHTML += msg + "<br>";
}