Unit 8: Preserving state

Unit Essential Questions

You should use the questions below to assess your own learning and understanding of course material. These questions are also a good method of organization if you’re taking notes.

Cookies and Sessions

  • What are cookies and what purpose do they serve?
  • What are sessions and what purpose do they serve?
  • What’s the difference between a persistent cookie and a session cookie and when should each be used?
  • What’re some legal considerations developers should be aware of when their sites use cookies?

Future of cookies

  • What might be the future of cookies?

Unit Readings

Cookies and Sessions

Check your understanding

If I store data on MyFakeSite.com using localStorage, close the browser, and then open a new window to visit MyFakeSite.com, will my data still be in my browser?

Click here to reveal the answer! Yes! localStorage has no default expiration so the data is stored even after you close the tab/window/browser. On the other hand, sessionStorage expires after a tab/window/browser is closed.
Programming Exercise

This optional exercise will have you practice with localStorage and sessionStorage.

Complete the code in this fiddle to store the inputs to the browser and then display the results. You should only need to update the JS code inside the saveInfo() and updateScreen() functions. Notice the difference when using localStorage vs sessionStorage:

  • When using localStorage, the most recently used name and job should appear when you open the same fiddle in a new tab/window
  • When using sessionStorage, null will show up in lieu of a name and job when you open the same fiddle in a new tab/window

Note: While localStorage normally stores data indefinitely, because we’re working through JSFiddle’s servers (which for security clear out our storage) if you were to quit your browser completely and then re-open the fiddle, you wouldn’t have the local data stored anymore. Hence, when testing your fiddle, make sure to simply open a new tab or window with the same link rather than closing the browser completely.

Future of cookies

ExpressJS and Cookies [optional]

The readings below might be particularly useful if you’re planning on using NodeJS and working with something like user logins for your final project.

Additional Resources

Cookies and Sessions

ExpressJS and Cookies

  • Watch Learn how to create a simple login app using ExpressJS + sessions: Part 1 and Part 2

results matching ""

    No results matching ""