The DOM Storage key() Method is related to the Storage object and is used to return the name of the key with the specified index. Storage object can be either a localStorage or sessionStorage Object.
Syntax:
- Local storage:
localStorage.key(index)
- Session storage:
sessionStorage.key(index)
Parameters: It accepts a parameter i.e. index to get the name of the key with that particular given index.
Return Values: It returns the name of the key in the form of the string.
Below is the HTML code to show the working of HTML DOM Storage key() Object:
Example:
HTML
<!DOCTYPE html> < html >
< head >
< title >
HTML DOM Storage key() Method
</ title >
<!-- Script to get the name of the key -->
< script >
function myGeeks() {
var key = localStorage.key(0);
document.getElementById(
"geeks").innerHTML = key;
}
</ script >
</ head >
< body >
< h1 >GeeksForGeeks</ h1 >
< h2 >DOM Storage key() Method</ h2 >
< br >
< button onclick = "myGeeks()" >
Submit
</ button >
< p id = "geeks" ></ p >
</ body >
</ html >
|
Output:
- Before Click:
- After Click:
Supported Browsers: The browser supported by DOM Storage key() Method are listed below:
- Google Chrome 4
- Edge 12
- Internet Explorer 8
- Firefox 3.5
- Opera 10.5
- Safari 4