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.
</body> tag like so:
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.
localStore.set(key (String), val (String))
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:
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");
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
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:
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:
- 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