[thelist] JS cookie question (ignore previous)

Tom Dell'Aringa pixelmech at yahoo.com
Sat Jan 11 13:34:21 CST 2003


--- Keith <cache at dowebscentral.com> wrote:
> I think your problem is in using a substring search method for
> finding the
> cookie name-value pair ("pid1" is also found in the strings "pid11"
> and
> "pid111" etc.). If your users are using Version 3 or better
> browsers use
> the array method for finding cookies instead. And instead of
> looking for
> 100 hypothetical pid+number cookies just look at cookies containing
> "pid".
>
> The following
> 1) puts all cookies into the "cookies" array
> 2) loop through that array and parses a name=value string for any
> cookies
> containing "pid"
> 4) alerts each name=value pair
>
>   getCookieData("pid")
>
> function getCookieData(obj){
> cookies=document.cookie.split(";");
>          for(i=0;i<cookies.length;++i){
>                  if(cookies[i].indexOf(obj) != -1){
>                  thisCookie=cookies[i].split("=")
>                  alert(thisCookie[0]+"="+thisCookie[1])
>                  }
>          }
> }

Keith that's great - just what I needed! Only 1 thing, I set up the
following code below and simply used a fake string for the cookie (to
test it). It worked perfectly except that it ran through the loop
TWICE instead of once. I cannot for the life of me figure out why...

----------------------------------
x = getCookieData("pid")

function getCookieData(obj)
{
coo = "aadf=asdf; asd%lk=23; pid111=2; pid231=4";

cookies = coo.split(";");    //document.cookie.split(";");
	for(i=0;i<cookies.length;++i)
	{
		if(cookies[i].indexOf(obj) != -1)
		{
			thisCookie=cookies[i].split("=")
			alert(thisCookie[0]+"="+thisCookie[1])
		}
	}
}
-----------------------------------

Any idea?

Tom

=====
var me = tom.pixelmech.webDeveloper();

http://www.pixelmech.com/
http://www.maccaws.com/
[Making A Commercial Case for Adopting Web Standards]

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



More information about the thelist mailing list