I think you may be misreading the redeclaring variables note. That just means that if you do something like var i = 23; var i;
i will still be 23, not reset to empty. If you do a redeclaration with an explicit value setting, like var i = 23; var i = 26;
then i will still get reset to 26.
There's also the issue of scope, where if you call a function and set a variable to a value, then when the function exits that variable will drop out of scope. So when the function is called again the variable will have to get reinitialized. If you don't want that to happen, you can declare the variable at a higher scope, say for the page, and then you can use your page-scoped variable in the function and when the function is called again, the variable should still have the value it was set to before.
Re the cookies, I don't think that you're setting your cookies correctly there. document.cookie isn't just a plain variable like myObj.myValue; it's an interface to the browser's cookies for the page. So you can't set it to just anything. You need to use at minimum a name/value pair, so something like document.cookie="imageValues=" + myJSON_2;. And then if you follow that up with something like document.cookie = "anotherValue=something";, then document.cookie will actually now be imageValues=whateverjsonitwas; anotherValue=something. Look at a page like this one at w3schools for more information.
...While I'm at it, I don't think that your JSON syntax is going to work there. You probably want something more like var myObj_1= { First_Image_x_1: First_Image_x_1, First_Image_y_1: First_Image_y_1, Second_Image_x_1: Second_Image_x_1, Second_Image_y_1: Second_Image_y_1 }; var myJSON_1= JSON.stringify(myObj_1);
no subject
I think you may be misreading the redeclaring variables note. That just means that if you do something like
var i = 23;
var i;
i will still be 23, not reset to empty. If you do a redeclaration with an explicit value setting, like
var i = 23;
var i = 26;
then i will still get reset to 26.
There's also the issue of scope, where if you call a function and set a variable to a value, then when the function exits that variable will drop out of scope. So when the function is called again the variable will have to get reinitialized. If you don't want that to happen, you can declare the variable at a higher scope, say for the page, and then you can use your page-scoped variable in the function and when the function is called again, the variable should still have the value it was set to before.
Re the cookies, I don't think that you're setting your cookies correctly there.
document.cookie
isn't just a plain variable likemyObj.myValue
; it's an interface to the browser's cookies for the page. So you can't set it to just anything. You need to use at minimum a name/value pair, so something likedocument.cookie="imageValues=" + myJSON_2;
. And then if you follow that up with something likedocument.cookie = "anotherValue=something";
, thendocument.cookie
will actually now beimageValues=whateverjsonitwas; anotherValue=something
. Look at a page like this one at w3schools for more information....While I'm at it, I don't think that your JSON syntax is going to work there. You probably want something more like
var myObj_1= {
First_Image_x_1: First_Image_x_1,
First_Image_y_1: First_Image_y_1,
Second_Image_x_1: Second_Image_x_1,
Second_Image_y_1: Second_Image_y_1
};
var myJSON_1= JSON.stringify(myObj_1);
Hope this helps.