Notes

Cookies and Web Storage

Cookies

What is a cookie?

What are cookies used for?

How to create a cooke in Javascript document.cookie = aNewCookieHere;

const firstCookie = "favoriteCat=million";
document.cookie = firstCookie;
document.cookie; // Returns "favoriteCat=million"
document.cookie = "favoriteCat=; expires = Thu, 01 Jan 1970 00:00:00 GMT";
document.cookie; // ""

Web Storage API

let field = document.getElementById("field");

if (sessionStorage.getItem("autosave")) {
  field.value = sessionStorage.getItem("autosave");
}

field.addEventListener("change", function () {
  sessionStorage.setItem("autosave", field.value);
});
if (!localStorage.getItem("bgcolor")) {
  populateStorage();
}
setStyles();

const populateStorage = () => {
  localStorage.setItem("bgcolor", document.getElementById("bgcolor").value);
  localStorage.setItem("font", document.getElementById("font").value);
  localStorage.setItem("image", document.getElementById("image").value);
};

const setStyles = () => {
  var currentColor = localStorage.getItem("bgcolor");
  var currentFont = localStorage.getItem("font");
  var currentImage = localStorage.getItem("image");

  document.getElementById("bgcolor").value = currentColor;
  document.getElementById("font").value = currentFont;
  document.getElementById("image").value = currentImage;

  htmlElem.style.backgroundColor = "#" + currentColor;
  pElem.style.fontFamily = currentFont;
  imgElem.setAttribute("src", currentImage);
};
  1. Our conditional checks to see if our local storage contains a data item called bgcolor
  2. If it does contain the data:
  3. If it does not contain the data:

When would we use the Web Storage API?


JSON

JSON is an open-standard file format that “uses human-readable text to transmit objects consisting of key-value pairs and array data types”.

JSON is a format!

Javascript Literal Value JSON
true “true”
false “false”
12.34 “12.34”
null “null”

String literals in JSON

Using the Built-in JSON Object

const array = [1, 'hello, "world"', 3.14, { id: 17 }];
console.log(JSON.stringify(array));
// prints [1, "hello, \"world\"", 3.14, {"id":17}]
const str = '[1,"hello, \\"world\\"",3.14,{"id":17}]';
console.log(JSON.parse(str));
// prints an array with the following entries:
//   0: 1
//   1: "hello, \"world\""
//   2: 3.14
//   3: { id: 17 }

Brain Teaser What will the following print?

const a = [1, 2, 3, 4, 5];
console.log(a[0]); // 1

const s = JSON.stringify(a);
console.log(s[0]); // [

const v = JSON.parse(s);
console.log(v[0]); /// 1

Web Storage

Storing Data in Local Storage

Reading Data in Local Storage

localStorage.setItem('eatz', 'I <3 falafel');
localStorage.setItem('coffee', 'black');
localStorage.setItem('doughnuts', '["glazed", "chocolate", "blueberry",
"cream-filled"]');

const eatz = localStorage.getItem('eatz');
const coffee = localStorage.getItem('coffee');
const doughnuts = localStorage.getItem('doughnuts');

console.log(eatz); // 'I <3 falafel'
console.log(coffee); // 'black'
console.log(doughnuts); // '["glazed", "chocolate", "blueberry", "cream-filled"]'

JSON and Local Storage

const doughnuts = JSON.parse(localStorage.getItem("doughnuts"));