storage.js is a library to easily manage data storage of key/value pair in browsers.
With storage.js can manage:
- localStorage to store data and retrieve them even if you closed your browser.
- sessionStorage to store data only for the current session.
- cookies if the localStorage is not available.
storage.js is a standalone library, it works well with any other JavaScript library like jQuery, jQueryMobile, Prototype, MooTools...
To use storage.js library you just need to include it in your html documents.
<script src='storage-1.2.0.min.js'></script>
To manage the localStorage/cookies you need to call the object storage
.
To manage the sessionStorage you need to call storage.session
.
###Example
storage.set(key, value); //set a new value in the localStorage (or cookies)
storage.session.set(key, value); //set a new value in the sessionStorage
Saves a value to localStorage/sessionStorage.
key
needs to be string or a number otherwise an exception is raised.
value
can be any JavaScript type.
storage.set(key, value);
Get the value from the localStorage/sessionStorage.
key
needs to be string or a number otherwise an exception is raised.
value = storage.get(key);
Check if the key is existing in the localStorage/sessionStorage.
key
needs to be string or a number otherwise an exception is raised.
storage.exists(key);
Remove the selected key or array of keys from the storage.
keys
needs to be string or a number or and Array of strings otherwise an exception is raised.
storage.remove(key);
storage.remove([key1, key2, ...]);
Clear the localStorage/sessionStorage.
storage.clear();
Rename the selected key by the newKey name.
If the newKey exists an exception will be raised except if the overwrite parameter is set to true.
key
needs to be string or a number otherwise an exception is raised.
newKey
needs to be string or a number otherwise an exception is raised.
key
(optional) Boolean.
storage.rename(key1, key2);
storage.rename(key1, key2, true);
Copy the keys to the other storage.
localStorage -> sessionStorage or sessionStorage -> localStorage
key
(optional) needs to be string or a number otherwise an exception is raised.
storage.copyToOtherStorage();
storage.copyToOtherStorage(key1, key2);
Get all the stored keys.
storage.getKeys();
storage.js supports all major browsers:
- Internet Explorer 8+
- Firefox 3.5+
- Safari 4+
- Chrome 4+
- Opera 10.5+
This library is released under the MIT License.