Get the value of the specified local storage item:
var x = localStorage.getItem("mytime");
This example demonstrates how to use the getItem()
method to get the value of a specified local storage item.
<!DOCTYPE html> <html> <body> <button onclick="createItem()">Create local storage item</button> <button onclick="myFunction()">Get the item value</button> <p id="demo"></p> <script> function createItem() {//from www . java 2 s. c om localStorage.mytime = Date.now(); } function myFunction() { var x = localStorage.getItem("mytime"); document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The getItem()
method returns value of the specified Storage Object item.
The getItem()
method belongs to the Storage Object, which can be either a localStorage object or a sessionStorrage object.
getItem(keyname);
Parameter Values
Parameter | Description |
---|---|
keyname | Required. A String specifying the name of the key you want to get the value of |
The getItem()
method returns a String representing the value of the specified key.