LocalStorage helper function which uses cookies as fallback.
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.md
localstore.js
localstore.min.js

README.md

localStore

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.

Usage

Firstly, include the javascript file on your page just below the final </body> tag like so:

<script src="http://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.

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:

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'.

localStore.get(key (String))

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.

localStore.del(key (String))

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.

localStore.clear()

This method will clear all values stored on this users computer for this domain. An example:

ls.clear();

To Do

  • Possibly create more methods to help with various storage issues (Any ideas what would be useful?)

Changelog

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