[thelist] JS cookie question (ignore previous)

Hans-Fr=?ISO-8859-1?B?6WTp?=ric Fraser hfraser at videotron.ca
Fri Jan 10 12:57:00 CST 2003


On 1/10/03 12:51 PM, "Tom Dell'Aringa" <pixelmech at yahoo.com> wrote:
> Here is my getCookieData() function, courtesy of Danny Goodman:
>
> ----------------------------------------------
> function getCookieData(labelName)
> {
> var labelLen = labelName.length;
> // read cookie property only once for speed
> var cookieData = document.cookie;
> var cLen =  cookieData.length;
> var i = 0;
> var cEnd;
>
> while(i < cLen)
> {
> var j = i + labelLen;
> if(cookieData.substring(i,j) == labelName)
> {
> cEnd = cookieData.indexOf(";", j);
> if(cEnd == -1)
> {
> cEnd = cookieData.length;
> }
> return unescape(cookieData.substring(j+1, cEnd));
> }
> i++
> }
> return "";
> }
> -----------------------------------------
>
> Might it have something to do with the cookie length? Its odd to get
> results with the = sign in it...

it is exactly that you did not take the = sign into account!
here is a basic function i made to get simple cookie information!


function getCookieData(whatCookie){
    var myCookie = new String(document.cookie);
    var myCookieHeader = whatCookie+"=";
    var myCookieStart = myCookie.indexOf(myCookieHeader);
    var myCookieEnd = myCookie.indexOf(';',myCookieStart);

    // if there is only one cokie you do not have the semicolon at the end
    if (myCookieEnd == -1){
        myCookieEnd = myCookie.length;
    }
    // extract data and check if the cookie exist
    if( myCookie.indexOf(myCookieHeader) != -1){
        myCookieStart = myCookieHeader.length;
        return myCookie.substring(myCookieStart, myCookieEnd);
        }
    else{
        // if the cookie does not exist well ... do what ever you want
        return 'noCookie';
    }
}

have fun!

hans!




More information about the thelist mailing list