PDA

View Full Version : cookie array question


tisha
12-07-2005, 06:20 AM
Hi,
I've got an object that holds an array of all available cookies on a page.
I want to loop through that array, search for a set of cookies that I know by name. The may or may not be there in addition to other cookies. What I want to do is remove any cookies that are not the 5 that I want from the ojbect array, so that the array will only hold the 5 cookies that I want to deal with.

Only I'm not sure how to do this. I know this is probably totally off, but my guess is something like this...


<script>
var cookList = new cookieList(); //function that gathers all cookies on the page
for(i in cookList) {
if (cookList[i]) {
var ca = findCookieOjbect("RA", cookList); //function that will find a named cookie in the cookieList array
var cu = findCookieObject("CU", cookList);
var r = findCookieObject("R", cookList);
var ro = findCookieOjbect("CA", cookList);
var sp = findCookieOjbect("SP", cookList);
}
}
cookList[0] = ca;
cookList[1] = cu;
cookList[2] = r;
cookList[3] = ro;
cookList[4] = sp;

cookList.split(5, <5); // don't know if I can do greater than 5 here
return cookList;
</script>

PhotoJoe47
12-07-2005, 07:23 AM
It must be the season for cookies:)

Here is a good tutorial on session cookies. On the second page, near the bottom there is a link to another page where you can download a set of prebuilt javascript cookie functions.

http://www.javascriptkit.com/javatutors/cookie.shtml

link to the prebuilt javascript cookie functions:

http://www.braemoor.co.uk/software/cookies.shtml

You might want to consider reading the document.cookie object for the five cookies you are looking for and add them to your array one at a time instead of putting all of the cookies into your array then removing the ones you don't want.

In that set of javascript cookie functions there is one called:

getCookieValue(cookieName)

Now if you put the 5 cookie names that you are looking for in an array like this:
var arrCookieName = new Array("RA","CU","R","CA","SP");
var arrCookieValue = new Array();

Then use a for loop.

for(x = 0; x<5;x++)
{
arrCookieValue[x]=getCookieValue(arrCookieName[x]);
}

That should give you an array with the value of your five cookies if they are there. If any of the cookies are not there that element in the arrCookieValue array will have a boolean value of "false".