Global object “localStorage”

Object > Storage

The localStorage object allows storing key-value pairs in a persistent store. Both keys and values are stored as strings.

In addition Tabris.js adds support for a secureStorage available in the global scope. This is a drop-in replacement for localStorage that keeps data encrypted using the Keychain on iOS and the AndroidKeyStore on Android 6+. Note that on Android 5 the store is encrypted but does not use hardware encryption.

The localStorage is only meant to store relatively short strings. To store larger amounts of data it is recommended to use the FileSystem API.

Type: Storage extends Object
Constructor: private
Singleton: localStorage
Namespace: global
Direct subclasses: None
JSX Support: No

Examples

JavaScript

localStorage.setItem('message', 'Hello World!');
console.log(localStorage.getItem('message')); // "Hello World!"

See also:

JSX A example how to read and write from the localStorage [► Run in Playground]

Methods

clear()

Remove all key/value pairs from the storage.

Returns: undefined

getItem(key)

Retrieves the value associated with the given key.

Parameter Type Description
key string  

Returns: string

key(index)

Returns the name of the key at the given index in the storage. The order of keys is platform dependent, you should not rely on it to be deterministic

Parameter Type Description
index number  

Returns: string

removeItem(key)

Removes the given key/value pair from the storage.

Parameter Type Description
key string  

Returns: undefined

setItem(key, value)

Stores a string value using the given key.

Parameter Type Description
key string  
value string  

Returns: undefined

Properties

length

The number of items in the storage.

Type: number
Settable: No