Memory-backed storage that implements the Web Storage API, making it a drop-in replacement for localStorage and sessionStorage in environments where these are not available.
Project website
- memorystorage.js (~3kB, commented)
- memorystorage.min.js (~2kB, minified)
- memorystorage.min.js.map (~2kB, debug map file)
memorystorage can be used directly from CDN, or from a local script file.
<script src="https://cdn.rawgit.com/download/memorystorage/0.9.7/dist/memorystorage.min.js"></script>Download memorystorage.min.js, place it in a folder lib in the root of your website and include it like this:
<script src="lib/memorystorage.min.js"></script>The MemoryStorage function creates (or returns) a storage object implementing the W3C Web Storage API.
By default, scripts share a global storage object, so scripts can access and mutate each other's store
object. To have MemoryStorage create a storage object that is isolated from other scripts, you pass in
a unique ID which acts as a namespace:
var isolated = new MemoryStorage('my-app'); // isolated from other scripts, recommended.If you don't pass in an ID, or use the ID 'global', you get a globally shared storage object:
var global = new MemoryStorage(); // will default to a globally shared storage object.
var global2 = new MemoryStorage('global'); // effectively same as aboveFor your convenience, the constructor permits new-less invocation:
var store = MemoryStorage('my-store');
var global = MemoryStorage();Instances of MemoryStorage expose an immutable id property that is set to
the id the store was created with:
alert(store.id); // alerts 'my-store'
alert(global.id); // alerts 'global'store.setItem('myString', 'Hello MemoryStorage!');
store.myObject = JSON.stringify({my: 'object'}));
alert(store.getItem('My string')); // alerts 'Hello MemoryStorage!'
alert(store['My string']); // alerts 'Hello MemoryStorage!'
alert(store.length); // alerts '2'
alert(store.key(1)); // alerts 'My object'
store.removeItem('My string');
alert(store.length); // alerts '1'
store.clear();
alert(store.length); // alerts '0'MemoryStorage is type-agnostic; it doesn't care about the type of data you store. If you want to remain within the Web Storage API, you should only read and write strings, however if you want you can store other types just as well:
store.myObject = {my: 'object'};
alert(store.myObject.my); // alerts 'object'
var tree = {
nested: {
objects: {
working: 'Sure!'
}
}
}
store.setItem('tree', tree);
alert(store.tree.nested.objects.working); // alerts 'Sure!'Copyright 2015 by Stijn de Witt. Some rights reserved.
Licensed under the Creative Commons Attribution 4.0 International (CC-BY-4.0) Open Source license.