Javascript保存本地数据 [英] Javascript Save Local Data

查看:71
本文介绍了Javascript保存本地数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Javascript和一个非常基本的html页面创建了一个本地网页游戏,所以我不需要并且想要为我的游戏使用服务器!

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 !

但是现在我想保存一些数据(例如记录)。所以我制作了一个Json文件,我设法使用 $。getJSON 阅读,我想知道我是否可以仅使用Javascript在我的json文件中写入?

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 ?

我不想使用任何PHP脚本!

I don't want to use any php script !

我期待你的回复!

推荐答案

假设您有一个名为得分的结构。

Supposing you have a structure named scores.

您可以对其进行字符串化并将其存储在 localStorage

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']) : [];

这篇关于Javascript保存本地数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆