Version: 0.2
localStore is a tiny (943 bytes gzipped, 1,038 bytes uncompressed) helper function that aims to simplify use of localStorage and also provide a fallback to users whose browsers don't support localStorage.
Firstly, include the javascript file on your page just below the final </body>
tag like so:
<script src="https://github.com/betamax/localStore/raw/master/localstore.min.js"></script>
Then you need to initialise the function into a variable in your javascript code:
var ls = new localStore;
Now that everything is set up, you can start using the function! It consists of 'set', 'get', 'del' and 'clear' methods.
This method requires a key and a value. The key is a unique (to your domain) identifier that will be used when getting, setting or deleting the item. The value is the data that you are storing. Example:
ls.set("userid", "203948");
Now stored on the users computer using either localStorage or a cookie will be the item 'userid' with the value '203948'.
This method requires a key; it returns a string or null. Use this method to fetch the value of an item in the storage that you have already set. For example to get the value of the "userid" key that was set above:
var user_id = ls.get("userid");
The user_id
variable will now contain the 203948
value that was set earlier. If the value was not ever set then user_id
would be null
.
This method requires a key. It will attempt to delete an item in the storage with the key you specified. To delete the userid item that we have been using as an example, do the following:
ls.del("userid");
There will now be no reference to the userid value stored for this user.
This method will clear all values stored on this users computer for this domain. An example:
ls.clear();
- Possibly create more methods to help with various storage issues (Any ideas what would be useful?)
Version 0.2 - 31/08/12
- Updated the method of checking for compatility to the method Modernizr currently uses (thanks @mitiya)
Version 0.1 - 14/10/10
- Added the script to GitHub