Dactaxx
Dactaxx

Reputation: 9

Save data in JS

I want to be able to save data to a database or an external file in JavaScript. For example; you can have a high score variable in pong. The program checks if your current score is higher than the high score variable and changes when your score is higher. This is cool, but once you reload the tab, everything is reset. Anything that works I will except. Even changing the source code of the program is fine

Upvotes: 0

Views: 7954

Answers (1)

Drew Noakes
Drew Noakes

Reputation: 310832

If you want to do this client-side, your options are basically:

Local storage is probably simplest:

var score = 1;
localStorage.score = 1;

Then later:

var score = parseInt(localStorage.score);

Note that everything you store in local storage is treated as a string. If you have a complex object, you'll have to convert it to JSON and back:

var players = {left:"tarzan", right:"jane"};
localStorage.score = JSON.stringify(players);

Then later:

var players = JSON.parse(localStorage.players);

Upvotes: 5

Related Questions