最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery setting a local storage variable - Stack Overflow

programmeradmin1浏览0评论

I'm having some trouble getting a local storage variable to store the proper value. The jist of it is I want to display the contents of the local variable, then if the user clicks, it pulls the data from an .xml file, saving it to a local variable.

Problem is, that it doesn't save to the local variable properly. I have tried a variety of syntax to get it to work and I am out of ideas.

Test site for it is located at .html

Script Code:

function startAjax()
{
    $("#clickme").text("Calling server");
    $.ajax(
    {
        url: "xmlpage.xml",
        success: callbackFunction,
        error: errorFunction
    });
}

function callbackFunction(data, info)
{
    var titles = $(data).find("title");
    if (titles && titles.length)
    {
        $("#results").text("result:" + titles.text());
        localStorage.setItem('titles', #results.text());
    }
    else
        errorFunction(data, "No titles");
}

function errorFunction(data, info)
{
    $("#clickme").text("error occurred:" + info);
}

$(document).ready(function ()
{
    $("#results").text(localStorage.getItem('titles'));
});

I'm having some trouble getting a local storage variable to store the proper value. The jist of it is I want to display the contents of the local variable, then if the user clicks, it pulls the data from an .xml file, saving it to a local variable.

Problem is, that it doesn't save to the local variable properly. I have tried a variety of syntax to get it to work and I am out of ideas.

Test site for it is located at http://web.engr.oregonstate.edu/~todtm/assignment2.html

Script Code:

function startAjax()
{
    $("#clickme").text("Calling server");
    $.ajax(
    {
        url: "xmlpage.xml",
        success: callbackFunction,
        error: errorFunction
    });
}

function callbackFunction(data, info)
{
    var titles = $(data).find("title");
    if (titles && titles.length)
    {
        $("#results").text("result:" + titles.text());
        localStorage.setItem('titles', #results.text());
    }
    else
        errorFunction(data, "No titles");
}

function errorFunction(data, info)
{
    $("#clickme").text("error occurred:" + info);
}

$(document).ready(function ()
{
    $("#results").text(localStorage.getItem('titles'));
});
Share Improve this question edited Jul 20, 2013 at 3:10 Sri Harsha Chilakapati 12k6 gold badges53 silver badges91 bronze badges asked Jul 20, 2013 at 2:42 Marshall TigerusMarshall Tigerus 3,76411 gold badges39 silver badges71 bronze badges 3
  • Which browser are you attempting this in? In Chrome if you open developer tools (F12) and click the console you can simply type localStorage to see what's stored there. localStorage.setItem('name','value') is all you need to do. – Kyle Muir Commented Jul 20, 2013 at 2:45
  • You're saving nothing in localStorage, and that's what appears when the page refreshes. What's the problem? – Barmar Commented Jul 20, 2013 at 2:47
  • I changed #results.text() to 'nothing' for testing and didn't change it back before posting this code. Where 'nothing' is I need the value that gets assigned to results.text – Marshall Tigerus Commented Jul 20, 2013 at 2:50
Add a comment  | 

1 Answer 1

Reset to default 16

you have a syntax error, need to get

localStorage.setItem('titles', $('#results').text());

or

localStorage.setItem('titles', titles.text());
发布评论

评论列表(0)

  1. 暂无评论