Get the name of the first local storage item:
var x = localStorage.key(0);
Try it Yourself »
The key() method returns name of the key with the specified index.
The key() method belongs to the Storage Object, which can be either a localStorage object or a sessionStorrage object.
Method | |||||
---|---|---|---|---|---|
key() | 4 | 8 | 3.5 | 4 | 10.5 |
localStorage.key(
index)
sessionStorage.key(
index)
Parameter | Description |
---|---|
index | Required. A Number representing the index of the key you want to get the name of |
DOM Version: | Web Storage API |
---|---|
Return Value: | A String, representing the name of the specified key |
The same example, but using session storage instead of local storage.
Get the name of the first storage item:
var x = sessionStorage.key(0);
Try it Yourself »
Loop through each local storage item and display the names:
for (i = 0; i < localStorage.length; i++) {
x = localStorage.key(i);
document.getElementById("demo").innerHTML += x;
}
Try it Yourself »
StorageEvent reference: The key Property
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!