Jwams Jwams - 4 days ago 5
HTML Question

How do I serialize a JavaScript object and then write it to a JSON file wihout node.js?

How do I write a JSON Serialized JavaScript object to a text file to read later on in JavaScript without using Node.js?

I am trying to maintain state in my objects and my work does not allow node.js.

Thanks!

Answer

You cannot write to a local computer file system using JS (i.e. to a text file as you say in your original question.)

However, you can write to a persistent object, localStorage, which will allow you to store information between sessions. Like so:

localStorage.setItem('key', 'value')

This information persists betweens sessions, so you can later call getItem to retrieve information:

localStorage.getItem('key') // === 'value'

This way, you could store information in the local browser. Not all browsers support this - for more information, check the MDN.

Comments