Local Storage and JSON

zombiejson.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>JavaScript Example</title>
</head>
<!-- when the page unloads (user goes somewhere else), call
     the saveZombie() function to save our zombie to local storage -->
<body onunload="saveZombie()">
    <h1>JavaScript Example</h1>
	
	<script type="text/javascript">
	
	function Zombie(name, deathDate, food) {
		this.name = name;
		this.deathDate = deathDate;
		this.food = food;
		
		this.intro = function() { 
			return "I am " + name + ". Like " + food + "!"; 
		}
	}
	
	// Request a Zombie
	var name = prompt("Give me a zombie name");
	var dd = prompt("Give me a zombie death date");
	var food = prompt("Give me a zombie food");
	var zombie2 = new Zombie(name, dd, food);

    // Write Zombie as JSON
    document.write(JSON.stringify(zombie2));
    
    // Take a look at what happens when we stringify then parse
    var jsonZombie = JSON.stringify(zombie2);
    var zombie3 = JSON.parse(jsonZombie);
    
    // note zombie3 is not a Zombie and does not have the
    // intro() method.
    
    // Read from local storage
    // var jsonFromStorage = localStorage.getItem("zombie");
	
	function saveZombie() {
		localStorage.setItem("zombie", JSON.stringify(zombie2));
	}
	</script>
</body>
</html>