Get a Cookie Value





11
Date Submitted Sun. Oct. 9th, 2005 8:15 PM
Revision 1 of 1
Coder mattrmiller
Tags Cookie | Get | JavaScript
Comments 0 comments
Get a Cookie Value
// Gets cookie
function GetCookie(strName)
{
        // Declare variables
        var strArg = strName + "=";
        var nLength = strArg.length;
        var nCookieLength = document.cookie.length;
        var i = 0;
       
        // Go though cookies
        while (i < nCookieLength)
        {
                if (document.cookie.substring(i, i + nLength) == strArg)
                {
                        return GetCookieVal(i + nLength);
                }
                i = document.cookie.indexOf(" ", i) + 1;
                if (i == 0)
                {
                        break;
                }
        }
       
        return null;
}

// Get cookie value
function GetCookieVal(nOffset)
{
        // Declare variables
        var nEndString = document.cookie.indexOf(";", nOffset);
       
        // Get end
        if (nEndString == 1)
        {
                nEndString = document.cookie.length;
        }
       
        return unescape(document.cookie.substring(nOffset, nEndString));
}

Matthew R. Miller

www.bluecreststudios.com
=================
Matthew R. Miller

http://bluecreststudios.com
http://www.codeandcoffee.com

Comments

There are currently no comments for this snippet.

Voting