0

I have a JavaScript file with an Object inside. What I'm trying to do is change the Object variable values using another JavaScript file. However if i change the values and close and reopen the JavaScript file with an Object inside, the values revert back to its original value.

Is there any way to change the Object values and save them, so when i close and reopen the file it has the new values instead of the old ones.

JavaScript file with an Object:

var datax = {
    Temperature: 20,
    Light_Intensity: 40,
    button: 0,
    max1: 0,
    Counter: 0
};

A second JavaScript file that changes datax Object values:

//Temperature
max = max / 655.34;
max = Math.round(max);
datax.max1 = max; //Changes the datax object max1 value
$('#temp').html(datax["max1"] + "°C");
3
  • This is what files do, they store permanent state. Commented Jun 27, 2014 at 11:18
  • Why don't you use cookies or localstorage instead? Commented Jun 27, 2014 at 11:18
  • 1
    Sounds like Javascript abuse :) You need to store permanent somewhere other than in the code. That gets reloaded fresh every time. Options include 1) Cookies 2) on your server 3) local storage Commented Jun 27, 2014 at 11:18

1 Answer 1

3

You can use cookies or HTML5 Storage. I prefer the second option in your case.

https://github.com/carhartl/jquery-cookie

http://amplifyjs.com/api/store/

Simple example with amplify.

// First store the object.
amplify.store( "yourObject", { foo: "bar" } );

// Then retrieve it.
var retrievedValue = amplify.store( "yourObject" );
Sign up to request clarification or add additional context in comments.

3 Comments

I also prefer the second option
please add some code to your answer, this will help other users to get an answer faster and will also prevent link rot
@AlexanderKludt Ready!

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.