Marius
Marius

Reputation: 5

Overwrite and save JavaScript variable values permanently

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");

Upvotes: 0

Views: 2346

Answers (1)

HolloW
HolloW

Reputation: 730

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" );

Upvotes: 3

Related Questions