This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

key

Summary

Returns the name of the nth key in the list.

Method of apis/web-storage/Storageapis/web-storage/Storage

Syntax

var  = object.key(index);

Parameters

index

Data-type
unsigned long

A zero-based index of the list entry, up to the length of the collection.

Return Value

Returns an object of type StringString

Examples

This example creates a new localStorage item (a timestamp) and sets it with a unique key, then executes a function, then retrieves the item’s name by its key index (0 in this case, as it is the first key) and reports its value.

window.localStorage.setItem('timestamp', (new Date()).getTime());
doSomethingElse();
startTimeKey = window.localStorage.key(0);
startTime = window.localStorage.getItem(startTimeKey);
alert("The doSomethingElse function was called at: " + startTime);

Notes

The returned key can be any string, including the empty string. The order of keys may change when items are added to the collection. This method will throw an “Invalid Argument” exception if index is out of range.

Related specifications

W3C Web Storage Specification
W3C Editor’s Draft

Attributions