0

I'm a beginner so I lack the proper vocabulary to clearly express what I want to try - or search for it so the right answers come up. But I'll try to explain:

I have a simple javascript application that has a bunch of variables that are filled with values from a database via jQuery ajax depending on initial user choices. Now, once the application is properly loaded with all the values retrieved from the db, I want to save it in this state for offline use.

The application would work fine without internet access if I hardcoded all the variable values, but as they depend on user choices upon start, this is not really an option. So I thought, maybe I could just save the loaded state as a js file. Is there any technology like that?

My fallback solution would be to do the hard work in php once the user clicks "download for offline use". Like keeping a copy of the js file as a string with placeholders for variable values and then replace them with the values from the database after the user made his initial choices. But to be honest, I feel like there must be a more elegant solution.

6
  • JavaScript files have no state. They're only text files. (Sorry if I misunderstood) Commented Aug 29, 2020 at 20:33
  • Sounds like you need a "backend" language. ... Just seeing you mention PHP. That would be the way to go. Not sure what you mean by "elegant" tho. Commented Aug 29, 2020 at 20:35
  • @GetSet I have yet to find anyone who can agree on what "elegant code" actually is. Commented Aug 29, 2020 at 20:38
  • Yes many takes on that subject. Commented Aug 29, 2020 at 20:40
  • I want to save it in this state for offline use with that requirement see @Aplet123 below Commented Aug 29, 2020 at 20:41

1 Answer 1

1

In browser Javascript, you cannot save files for security reasons. There is, however, an API called localStorage which allows you to store persistent information in the browser. You can put all your variables in an Object, stringify it to JSON, then save it in localStorage, then retrieve them by parsing the JSON:

// saving variables
var myVariables = {a: 1, b: "foo", c: [4, 5, 6]};
localStorage.setItem("vars", JSON.stringify(myVariables));
// retrieving variables
var retrievedVariables = JSON.parse(localStorage.getItem("vars"));
console.log(retrievedVariables.b); // "foo"
Sign up to request clarification or add additional context in comments.

2 Comments

That sound good, but does this require the user to keep the tab open or try to access the website while offline? Or can I create a 'clickable' file for him to open?` ... most users are not very techy and just need an icon to click on to start the application.
You can look into service workers to make the website available offline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.