Check for localStorage, catching exceptions - fixes #1291, fixes #1688.

Accessing `window.localStorage` can immediately throw an exception, which needs to be caught.
For more information, see http://www.w3.org/TR/webstorage/#dom-localstorage.