var cookieValue = document.getElementById("demo");
var value = cookieValue .getAttribute('value');
if(typeof(Storage)!=="undefined")
{
alert(value);
localStorage.setItem = ("GetData" , value);
alert(localStorage.setItem);
}
function loading()
{
alert("coming");
var allcookies = localStorage.getItem('GetData');
alert(allcookies);
}
Above is the way I am setting localStorage.setItem
and I am getting LocalStorage.
But Where I didn't get the output it is showing Null. Please Suggest me any Solution.
localStorage is a property that allows JavaScript sites and apps to save key-value pairs in a web browser with no expiration date. This means the data stored in the browser will persist even after the browser window is closed.
# View localStorage keys and values Click the Application tab to open the Application panel. Expand the Local Storage menu. Click a domain to view its key-value pairs. Click a row of the table to view the value in the viewer below the table.
parse dependency must be a string . But the local storage return type is string|null so it can be both string and null and when you declare the data, its value is null until you render the component (or call the function) and then call the getItem function, it gets the value, and then it's a string .
Your code should be :
var cookieValue = document.getElementById("demo");
var value = cookieValue .getAttribute('value');
if(typeof(Storage)!=="undefined")
{
alert(value);
localStorage.setItem("GetData" , value);
alert(localStorage.getItem("GetData"));
}
function loading()
{
alert("coming");
var allcookies = localStorage.getItem('GetData');
alert(allcookies);
}
OR
var cookieValue = document.getElementById("demo");
var value = cookieValue .getAttribute('value');
if(typeof(Storage)!=="undefined")
{
alert(value);
localStorage["GetData"] = value;
alert(localStorage["GetData"]);
}
function loading()
{
alert("coming");
var allcookies = localStorage["GetData"];
alert(allcookies);
}
The correct syntax for setItem
is
localStorage.setItem("GetData", value)
not
localStorage.setItem = ("GetData", value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With