Chapter 9Saving Data on the Client

Remember when cookies were awesome? Neither do I. Cookies have been rather painful to deal with since they came on the scene, but we put up with the hassle because they’ve been the only reliable way to store information on the clients’ machines.

To use cookies, we have to name them and set their expiration. This involves a bunch of JavaScript code we wrap in a function so we never have to think about how it actually works, kind of like this:

html5_localstorage/setcookie.js
 
// via http://www.javascripter.net/faq/settinga.htm
 
function​ SetCookie(cookieName,cookieValue,nDays) {
 
var​ today = ​new​ Date();
 
var​ expire = ​new​ Date();
 
if​ (nDays==null || nDays==0) nDays=1;
 
expire.setTime(today.getTime() ...

Get HTML5 and CSS3, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.