// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
// Store
localStorage.lastname = "Smith";
// Retrieve
document.getElementById("result").innerHTML = localStorage.lastname;
//the following example counts the number of times a user has clicked
//a button. In this code the value string is converted to a number to be
//able to increase the counter
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount) + 1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
localStorage.clickcount + " time(s).";
-
Stores the data with no expiration date.
-
The data will not be deleted when the browser is closed, and will be available the next day, week, or year.
-
Name/value pairs are always stored as strings. Remember to convert them to another format when needed!.