The standard way to read a multi-value cookie is into an associative array --
var cookie_data = new Array();
cookie_data = cookie_name.split(" | ");
This puts everything into name1=value1 pairs -- which makes it cumbersome to extract and replace values. First you must search for the string "name=" then do complex substring work to extract the value (20+ lines of code). To replace a value in the array is even harder, I haven't figured out how to do it yet, easily, that is.
Seems to me it is better to read a multi-item cookie into an OBJECT where the index position is known --
var cookie_data new Object;
Say we know there are 20 entries in the cookie data, the NAMES of each entry is '1' , '2' , '3', ... up to ... '20'
So in an object array,
var first_value = cookie_data ['1'];
var last_value = cookie_data ['20'];
gets the VALUES of the name=value pairs, using normal array indexing -- SO easy to understand and do.
This compares to arduous searching for "name=" strings to extract the following substring to get a value. Why do that if you already know the name-position of each item in the array, why not just index its value?
QUESTIONS --
(1) why not use an Object instead of an associative array, especially if the field names are known, like 1,2,3 etc.? If so, it is EASY to replace a known value with a new value -- cookie_data ['6'] = updated_value;
(2) If not, how do I update the value in an assoc. array easily, without a mass of code to search the array?
(3) Can name=value pairs be retrieved and replaced from an array OBJECT as I've shown above, or not?
(4) If not, please give CODE to show how it is done in an associative array -- I am struggling with it.
(5) Why does everyone use assoc. arrays if it is easier to index a value of an entry by -- array['name'] ?
NO LINKS. I searched > 200 articles on cookie arrays. NO ONE shows how to update/change a value in an associative array . If you know how to do it in either type of array, PLEASE POST YOUR CODE !!!
BTW, I don't want to change the cookies directly. Too many many changes / edits, so it must be in a JS array -- only after all editing, is the new cookie written back to a file (i.e. no direct cookie writing). Thanks.
Start Free Trial