Emmet1011
Emmet1011

Reputation: 61

How do I save JavaScript variables permenantly/persistently?

I have 5 arrays, which can all be updated by the user. They are structured as:

var nameStore = [
{ name: "Tim Jones", idn: "i0001" },
{ name: "Mark Gooderham", idn: "i0002" }
];

var nameStoreName;

var subjectStore = [
    { name: "Sailing", ids: "s0001" },
    { name: "Navigation", ids: "s0002" }
];

var subjectStoreName;

var classStore = [
    { name: "Class A", idc: "c0001" },
    { name: "Class 2", idc: "c0002" }
];

var classStoreName;

var roomStore = [
    { name: "Room 1", idr: "r0001" },
    { name: "Room 2", idr: "r0002" }
];

var weekStore = [
    { week: 1, weekTimes: ["mon01i0001s0001c0001r0001", "mon02i0001s0002c0002r0002"] },
    { week: 2, weekTimes: ["mon02i0002s0002c0002r0002"] },
];

I want to be able to store these arrays permanently, so when the webpage closes, the arrays will have their data saved, and can then be accessed by another user later. I know this is a big question, but even if you could just direct me to other resources, that would help.

Thanks.

Upvotes: 1

Views: 54

Answers (2)

stargazer
stargazer

Reputation: 21

For that purpose, you should store your arrays in a database. Before closing page, submit arrays to DB and update records. When user opens the webpage, query DB and get arrays' contents.

Upvotes: 1

ABGR
ABGR

Reputation: 5205

You can use localStorage to store the array on the browser if you're not using database to persist.

localStorage.setItem('key', JSON.stringify(arr));

var item = JSON.parse(localStorage.getItem('key'));

Upvotes: 0

Related Questions