[TYPO3-dev] Set and read cookies

bernd wilke xoonsji02 at sneakemail.com
Wed Jul 16 18:14:29 CEST 2008


on Sun, 13 Jul 2008 22:24:06 +0300, Tapio Markula wrote:

> Hi
> 
> I would need in an extension JS for cookies
> 
> http://www.willmaster.com/library/javascript/set_read_cookies.php
> 
> the problem is that I would nee read cookie for *two-level* array cookie
> but examples seems to handle just one level simple cookies.
> 
> How to create ReadCookie for two-level cookie, for example for this
> 
> 
> 	function saveLevel2(value) {
> 		var name = "menuCookie[level2Cookie]"; var date = new Date
();
> 		date.setTime(date.getTime()+(24*60*60*1000)); var expires 
= ";
> 		expires="+date.toGMTString(); document.cookie =
> 		name+"="+value+expires+"; path=/";
> 	}

do you really need arrays?

may this could be solved with multiple cookies, where teh cookie-names 
can be generated:

menuCookie
menuCookie_1
menuCookie_2
   :

var name = "menuCookie_"+level; 

and if you need assoziative arrays (they are no real arrays), you have to 
work with two (pseudo-)arrays:

menuCookie_key_0 = 'first keyword';  menuCookie_val_0 = 'first value';
menuCookie_key_1 = 'second keyword'; menuCookie_val_1 = 'second value';

with functions you can read and write your array-variables as cookies

something like: 
(key-separation by '__' instead of two numeric arrays,
for a numeric array you can use a normal counter.)

function writeAssociativeArrayToCookie(a,aname) {
	var date = new Date();
	date.setTime(date.getTime()+(24*60*60*1000)); 
	var expires = ";expires="+date.toGMTString(); 
	for (var arrayKey in a)
		document.cookie =aname+'__'+arraykey+"="+a[arrayKey]
	                        +expires
	                        +";path=/";
}

reading single cookies is a little bit more complicated in javascript and 
is done normaly in a function, where you can handle your 'arrays' in a 
similar way.

bernd
-- 
http://www.pi-phi.de/t3v4/cheatsheet.html




More information about the TYPO3-dev mailing list