/ Published in: JavaScript

Here's how to use this:
* To access something in the local storage, type `Evan.local('name')`. You may need to use `JSON.parse()` on this result. Returns `null` if nothing's there.
* To assign something in the local storage, type `Evan.local('name', value)`.
* To remove something from the local storage, type `Evan.local('name', null)`.
This may not work if private browsing is enabled (eg, on Safari for iOS).
* To access something in the local storage, type `Evan.local('name')`. You may need to use `JSON.parse()` on this result. Returns `null` if nothing's there.
* To assign something in the local storage, type `Evan.local('name', value)`.
* To remove something from the local storage, type `Evan.local('name', null)`.
This may not work if private browsing is enabled (eg, on Safari for iOS).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Accessor: local('key') // Setter: local('key', value) // Destroyer: local('key', null) Evan.local = function(key, value) { // Getter if (arguments.length === 1) return localStorage.getItem(key); // Clear it no matter what (for iOS) localStorage.removeItem(key); // Setter if (value !== null) { if ((typeof value !== 'string') && (!(value instanceof String))) value = JSON.stringify(value); localStorage.setItem(key, value); } };
Comments
