Retrieving JSON data in web storage not working

Possible Duplicate:
Storing Objects in HTML5 localStorage

I'm trying to store JSON data, name & phonenumber given in two text fields and then later(after page refresh) retrieve and print the data on the same fields with following code.

        function saveData() {
            var saveD = { 
                name: document.getElementById("name").value,
                phone: document.getElementById("phone").value
            }; 

            window.localStorage.setItem("info", saveD);
        } 
        var storedData = window.localStorage.getItem("info");

        document.getElementById("name").value = storedData.name;
        document.getElementById("phone").value = storedData.phone;

What is wrong? I get "undefined" on both fields.


Save like this:

window.localStorage.setItem("info", JSON.stringify(saveD));

And load like this:

var storedData = JSON.parse(window.localStorage.getItem("info"));

You have to store objects as JSON in local storage.

链接地址: http://www.djcxy.com/p/27936.html

上一篇: JavaScript保存到本地存储?

下一篇: 检索Web存储中的JSON数据无法正常工作