Javascript Save Local Data

不想你离开。 提交于 2019-12-21 06:05:00

问题


I made a Local Web Game using Javascript and a very basic html page so I don't need and want to use a Server for my game !

But now I want to save some data (record for example) . So i made a Json file which I managed to read using $.getJSON and I was wondering if I could write in my json file using only Javascript ?

I don't want to use any php script !

I look forward to your response !


回答1:


Supposing you have a structure named scores.

You can stringify it and store it in localStorage :

function addScore(newScore) {
    scores.push(newScore);
    localStorage['scores'] = JSON.stringify(scores);
}

And then restore it at start of your program :

var scores = localStorage['scores'] ? JSON.parse(localStorage['scores']) : [];



回答2:


Use HTML5 WebSQL as sql database or HTML5 Local Storage as no sql database.



来源:https://stackoverflow.com/questions/14157529/javascript-save-local-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!