Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the local Storage Value

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.

like image 459
srinu Avatar asked Jun 28 '12 14:06

srinu


People also ask

What is local storage value?

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.

How do I find local storage value in Chrome?

# 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.

How do I get local storage values in typescript?

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 .


2 Answers

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);

}
like image 58
Jerome Cance Avatar answered Sep 23 '22 17:09

Jerome Cance


The correct syntax for setItem is

localStorage.setItem("GetData", value)

not

localStorage.setItem = ("GetData", value)

like image 33
Brant Olsen Avatar answered Sep 21 '22 17:09

Brant Olsen