Setting up persistence.js in the browser

Browser support

  • Modern webkit browsers (Google Chrome and Safari)
  • Firefox (through Google Gears or in-memory database and localStorage fallback)
  • Opera
  • Android browser (tested on 1.6 and 2.x)
  • iPhone browser (iPhone OS 3+)
  • Palm WebOS (tested on 1.4.0)

Setup

To use persistence.js you need to clone the git repository:

git clone git://github.com/zefhemel/persistencejs.git

To use it you need to copy lib/persistence.js to your web directory, as well as any data stores you want to use. Note websql stores both depend on the sql store. A typical setup requires you to copy at least lib/persistence.jslib/persistence.store.sql.js and lib/persistence.store.websql.js to your web directory. You can then load them as follows:

<script src="persistence.js" type="application/javascript"></script>
<script src="persistence.store.sql.js" type="application/javascript"></script>
<script src="persistence.store.websql.js" type="application/javascript"></script>

If you want to use the in-memory store (in combination with localStorage) you also need the persistence.store.memory.js included.

Setup your database

You need to explicitly configure the data store you want to use, configuration of the data store is store-specific. The WebSQL store (which includes Google Gears support) is configured as follows:

persistence.store.websql.config(persistence, 'yourdbname',
                                'A database description', 5 * 1024 * 1024);

The first argument is always supposed to be persistence. The second in your database name (it will create it if it does not already exist, the third is a description for you database, the last argument is the maximum size of your database in bytes (5MB in this example).

You’re now ready to start using persistence.js!

See also  JavaScript security trends and best practices for 2024