View on GitHub

reading-notes

Reading notes for Codesfellows Coding

Persistence

HTML5 Storage

var foo = localStorage.getItem("bar");
// ...
localStorage.setItem("bar", foo);
var foo = localStorage["bar"];
// ...
localStorage["bar"] = foo;

interface Storage {
  readonly attribute unsigned long length;
  getter DOMString key(in unsigned long index);
};

If you call key() with an index that is not between 0–(length-1), the function will return null.

if (window.addEventListener) {
  window.addEventListener("storage", handle_storage, false);
} else {
  window.attachEvent("onstorage", handle_storage);
};

Therefore, to hook the storage event, you’ll need to check which event mechanism the browser supports.

function handle_storage(e) {
  if (!e) { e = window.event; }
}