Multiple Dimensional Arrays in Cookies

The following is reprinted here with the permission of the author.


Hi Stephen,

I am using your cookie toolbox and it is very handy so thank you very much!

Now I understand that it has no multidimensional array support, but I have worked out a way to do it. My method is as follows:

here I set the array, notice that ** delimeters

//set config cookie
  jc_params = document.forms['jc_config'].elements;
 
   jc_params_array = init_array();
  jc_params_array.splice(0,1);
 
  for(i=0; i    jc_params_array[i+1] = jc_params[i].id+'**'+jc_params[i].value;
  }
 
  var cookieName = 'jc_temp';
  var timeToKeep = 60000; // one minute
  var expires = new Date();
  expires.setTime(expires.getTime() + timeToKeep);
 
  set_array(cookieName, jc_params_array, expires);

When I access the cookie array I can simply use split() to get it back again.

function jc_initparams(){
  var cookieName = 'jc_temp';
  var jc_params_array = init_array();
  get_array(cookieName, jc_params_array);
 
  for (var i=1; i    results = jc_params_array[i].split('**');
 
  var param = results[0];
  var setting = results[1];
 
  jc_setparam(param, setting);
  }
}

This is handy if you need associative array support.

Hope this can help someone :)

Kindest regards,
Adam Docherty aka Lobos


 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate