xxxxxxxxxx
// localStorage for objects, arrays or any data type
var obj = {
firstName: "Bob",
lastName: "Jeff",
age: 13
}
localStorage.setItem("itemname", JSON.stringify(obj)); // Save the obj as string
var item = JSON.parse(localStorage.getItem("itemname"));
// ^^ Parse string then set `item` as the obj
xxxxxxxxxx
function createItem() {
localStorage.setItem('nameOfItem', 'value');
}
createItem() // Creates a item named 'nameOfItem' and stores a value of 'value'
function getValue() {
return localStorage.getItem('nameOfItem');
} // Gets the value of 'nameOfItem' and returns it
console.log(getValue()); //'value';
xxxxxxxxxx
localStorage.setItem('nombre_color', 'Rojo'); // envio de data al "localStorage"
localStorage.getItem("nombre_color");// captura de data del "localStorage"
localStorage.removeItem('nombre_color'); // Eliminar variable especifica
localStorage.clear(); // Eliminar todas las variables
xxxxxxxxxx
localStorage.setItem('user_name', 'Rohit'); //store a key/value
var retrievedUsername = localStorage.getItem('user_name'); //retrieve the key
xxxxxxxxxx
> Add item
localStorage.setItem('foo', 'bar');
> Get item
localStorage.getItem('foo');
> Remote item
localStorage.removeItem('foo');
> Clear all
localStorage.clear();
xxxxxxxxxx
function setItem(name, value) {
localStorage.setItem(name, value);
}
function getItem(name) {
localStorage.getItem(name);
}
function deletItem(name) {
localStorage.removeItem(name);
}
function clearStorage() {
localStorage.clear();
}
xxxxxxxxxx
localStorage.setItem("user_name", "Bob");
document.body.addEventListener("click", function(){
alert(localStorage.getItem("user_name"))
});
xxxxxxxxxx
localStorage.setItem(key, val);
var val = localStorage.getItem(key);
localStorage.removeItem(key);
localStorage.clear();
xxxxxxxxxx
// Check browser support
if (typeof(Storage) !== "undefined") {
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
xxxxxxxxxx
localStorage.setItem('Items' , 'Bread');
let Bread = localStorage.getItem('Items');