Build Status

localForage

// In localStorage, we would do:
localStorage.setItem('key', JSON.stringify('value'));
doSomethingElse();

// With localForage, we use callbacks:
localforage.setItem('key', 'value', doSomethingElse);

// Or we can use Promises:
localforage.setItem('key', 'value').then(doSomethingElse);
# In localStorage, we would do:
localStorage.setItem "key", JSON.stringify("value")
doSomethingElse()

# With localForage, we use callbacks:
localforage.setItem "key", "value", doSomethingElse

# Or we can use Promises:
localforage.setItem("key", "value").then doSomethingElse

Offline storage, improved.

localForage is a JavaScript library that improves the offline experience of your web app by using an asynchronous data store with a simple, localStorage-like API. It allows developers to store many types of data instead of just strings.

localForage includes a localStorage-backed fallback store for browsers with no IndexedDB or WebSQL support. Asynchronous storage is available in the current versions of all major browsers: Chrome, Firefox, IE, and Safari (including Safari Mobile).

localForage offers a callback API as well as support for the ES6 Promises API, so you can use whichever you prefer.

Download localforage.min.js

Installation

# Optional installation with bower:
bower install localforage
<script src="localforage.js"></script>
<script>localforage.getItem('my alert', alert);</script>

To use localForage, download the latest release or install with bower (bower install localforage).

Then simply include the JS file and start using localForage: <script src="localforage.js"></script>. You don’t need to run any init method or wait for any onready events.

Data API

These APIs deal with getting and setting data in the offline store.

getItem

localforage.getItem('somekey', function(value) {
    // Run this code once the value has been
    // loaded from the offline store.
    console.log(value);
});
localforage.getItem('somekey').then(function(value) {
    // The same code, but using ES6 Promises.
    console.log(value);
});
localforage.getItem "somekey", (value) ->
  # Run this code once the value has been loaded
  # from the offline store.
  console.log value

localforage.getItem("somekey").then (value) ->
  # The same code, but using ES6 Promises.
  console.log(value)

getItem(key, successCallback)

Gets an item from the storage library and supplies the result to a callback. If the key does not exist, getItem() will return null.

setItem

localforage.setItem('somekey', 'some value', function(value) {
    // Do other things once the value has been saved.
    console.log(value);
});

// Unlike localStorage, you can store non-strings.
localforage.setItem('my array', [1, 2, 'three'], function(value) {
    // This will output `1`.
    console.log(value[0]);
});

// You can even store binary data from an AJAX request.
request = new XMLHttpRequest();
request.open('GET', '/photo.jpg', true);
request.responseType = 'arraybuffer';

request.addEventListener('readystatechange', function() {
    if (request.readyState === 4) { // readyState DONE
        localforage.setItem('photo', request.response, function(img) {
            // This will be a valid blob URI for an <img> tag.
            var blob = new Blob([image]);
            var imageURI = window.URL.createObjectURL(blob);
        }
    }
});
localforage.getItem "somekey", "some value" (value) ->
  # Do other things once the value has been saved.
  console.log value

# Unlike localStorage, you can store non-strings.
localforage.setItem "my array", [1, 2, "three"], (value) ->
  # This will output `1`.
  console.log value[0]

# You can even store binary data from an AJAX request.
request = new XMLHttpRequest()
request.open "GET", "photo.jpg", true
request.responseType = "arraybuffer"

request.addEventListener "readystatechange", ->
  if request.readyState == 4 # readyState DONE
    localforage.setItem "photo", request.response, (img) ->
      # This will be a valid blob URI for an <img> tag.
      blob = new Blob [image]
      imageURI = window.URL.createObjectURL blob

setItem(key, value, successCallback)

Saves data to an offline store. You can store the following types of JavaScript objects:

removeItem

localforage.removeItem('somekey', function() {
    // Run this code once the key has been removed.
    console.log('Key is cleared!');
});
localforage.removeItem "somekey", ->
  # Run this code once the key has been removed.
  console.log "Key is cleared!"

removeItem(key, successCallback)

Removes the value of a key from the offline store.

clear

localforage.clear(function() {
    // Run this code once the database has been entirely deleted.
    console.log('Database is now empty.');
});
localforage.clear ->
  # Run this code once the database has been entirely deleted.
  console.log "Database is now empty."

clear(successCallback)

Removes every key from the database, returning it to a blank slate.

length

localforage.length(function(numberOfKeys) {
    // Outputs the length of the database.
    console.log(numberOfKeys);
});
localforage.length (numberOfKeys) ->
  # Outputs the length of the database.
  console.log numberOfKeys

length(successCallback)

Gets the number of keys in the offline store (i.e. its “length”).

key

localforage.key(2, function(keyName) {
    // Name of the key.
    console.log(keyName);
});
localforage.key 2, (keyName) ->
  # Name of the key.
  console.log keyName

key(keyIndex, successCallback)

Get the name of a key based on its ID.

keys

localforage.keys(function(keys) {
    // An array of all the key names.
    console.log(keys);
});
localforage.keys (keys) ->
  # An array of all the key names.
  console.log keys

keys(successCallback)

Get the list of all keys in the datastore.

Settings API

These methods allow driver selection and database configuration. These methods should generally be called before the first data API call to localForage ( i.e. before you call getItem() or length(), etc.)

setDriver

// Force localStorage to be the backend driver.
localforage.setDriver(localforage.LOCALSTORAGE);

// Supply a list of drivers, in order of preference.
localforage.setDriver([localforage.WEBSQL, localforage.INDEXEDDB]);
# Force localStorage to be the backend driver.
localforage.setDriver localforage.LOCALSTORAGE

# Supply a list of drivers, in order of preference.
localforage.setDriver [localforage.WEBSQL, localforage.INDEXEDDB]

setDriver(driverName)
setDriver([driverName, nextDriverName])

Force usage of a particular driver or drivers, if available.

By default, localForage selects backend drivers for the datastore in this order:

  1. IndexedDB
  2. WebSQL
  3. localStorage

If you would like to force usage of a particular driver you can use setDriver() with one or more of the following parameters.

config

// This will rename the database from "localforage"
// to "Hipster PDA App".
localforage.config({
    name: 'Hipster PDA App'
});
# This will rename the database from "localforage"
# to "Hipster PDA App".
localforage.config
  name: "Hipster PDA App"

config(options)

Set and persist localForage options. This must be called before any other calls to localForage are made, but can be called after localForage is loaded. If you set any config values with this method they will persist after driver changes, so you can call config() then setDriver(). The following config values can be set:

name
The name of the database. May appear during storage limit prompts. Useful to use the name of your app here.
Default: 'localforage'
size
The size of the database in bytes. Used only in WebSQL for now.
Default: 4980736
storeName
The name of the datastore. In IndexedDB this is the dataStore, in WebSQL this is the name of the key/value table in the database. In localStorage, this is used as a key prefix for all keys stored in localStorage.
Default: 'keyvaluepairs'
version
The version of your database. May be used for upgrades in the future; currently unused.
Default: 1.0
description
A description of the database, essentially for developer usage.
Default: ''