Search code examples
javascriptcordovastoremobile-application

How do I make JavaScript PhoneGap apps to remember something?


I want to make a JavaScript PhoneGap app being able to store information, like log in data or for a savegame, but obviously I'm not able to write into files with JavaScript ansd since this is supposed to work offline I am also not able to run a server script that manages it for me. I'm pretty sure there is a way to do this and I'd be very thankful for your help!


Solution

  • You can use local storage. Local storage only stores strings, so you'll probably want to serialize JSON when saving to local storage. See is a breakdown of local storage size by browser.

    var myData = { foo: "bar" };
    localStorage.setItem("myData", JSON.stringify(myData));
    
    var retrievedDataString = localStorage.getItem("myData");
    var retrievedData = JSON.parse(myDataString);