Can someone post code that can read/write cookie collections with keys?

Discussion in 'Javascript' started by Mike, Sep 7, 2004.

  1. Mike

    Mike Guest

    Hello,

    I can't find any javascript that reads and writes cookies with keys,
    so that it is compatible with ASP (I want to read and write cookies
    from both javascript and ASP)

    for example in ASP:
    <%
    Response.Cookies("user")("firstname")="John"
    Response.Cookies("user")("lastname")="Smith"
    Response.Cookies("user")("country")="Norway"
    Response.Cookies("user")("age")="25"
    %>

    I'm looking for javascript code that can read that information...

    Thanks,
    Mike
    Mike, Sep 7, 2004
    #1
    1. Advertising

  2. Mike

    kaeli Guest

    In article <>,
    enlightened us with...
    > Hello,
    >
    > I can't find any javascript that reads and writes cookies with keys,
    > so that it is compatible with ASP (I want to read and write cookies
    > from both javascript and ASP)
    >


    Is this what you're looking for?
    Extend it as needed for domain and such.
    Watch for word-wrapping.

    --------------- jsCookies.js -----------------------------------

    /* This file contains cookie functions. */
    /* File Functions:
    1. setCookie - writes cookie
    2. getCookie - gets value of cookie
    3. removeCookie - deletes a cookie
    4. detectCookies - checks if cookies are enabled
    */

    function setCookie(cookieName, cookieValue, expireDays)
    {
    /* Pass in three strings - the name of the cookie, the value, and the
    number of days until expiration.
    Pass in a "" empty string for expireDays to set a session cookie (no
    expires date).
    Pass in any other date for expire as a number of days to be added to
    today's date. */

    var expires = "";
    var re = /^\d+$/;

    /* make sure expireDays is a number */
    if (expireDays != "" && re.test(expireDays))
    {
    expires = new Date();
    expires.setDate(expires.getDate() + expireDays);
    expires = expires.toGMTString();
    }
    document.cookie = cookieName+"="+cookieValue+";expires="+expires;
    }

    function removeCookie (cookieName)
    {
    /* Pass in the name of the cookie as a string and it will be removed. */
    expires = Now();
    document.cookie = cookieName+"= ;expires="+expires.toGMTString();
    }

    function getCookie (cookieName)
    {
    cookieValue = ""
    if (document.cookie.indexOf(cookieName) == -1)
    {
    /* there is no cookie by this name for this user */
    return cookieValue;
    }
    else
    {
    /* get the beginning index of the cookie by looking for the cookie name
    */
    cookieStart = document.cookie.indexOf(cookieName);
    /* get the beginning index of the cookie value by looking for the equal
    sign after the name */
    cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
    /* get the end index of the cookie value by looking for the semi-colon
    after the value */
    cookieValEnd = document.cookie.indexOf(";", cookieStart);
    /* if no semi-colon, then use the whole length */
    if (cookieValEnd == -1)
    {
    cookieValEnd = document.cookie.length
    }
    /* use substring to get the text between the two indices and that is
    the value of the cookie */
    cookieValue = document.cookie.substring(cookieValStart, cookieValEnd);
    return cookieValue;
    }
    }

    function detectCookies()
    {
    /* function returns true if cookies are enables, false if not */
    setCookie("test", "test", "");
    tmp = getCookie("test")
    if (tmp != "test")
    {
    return false;
    }
    else
    {
    return true;
    }
    }

    --
    --
    ~kaeli~
    Well, aren't we just a flipping ray of sunshine?
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    kaeli, Sep 8, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Poland
    Replies:
    9
    Views:
    728
    VisionSet
    Sep 27, 2003
  2. Replies:
    3
    Views:
    11,127
  3. Replies:
    10
    Views:
    726
    Daniel T.
    Feb 3, 2006
  4. mutex
    Replies:
    0
    Views:
    212
    mutex
    Jul 27, 2003
  5. emilgo

    Read cookie with different cookie

    emilgo, Feb 13, 2004, in forum: Javascript
    Replies:
    0
    Views:
    92
    emilgo
    Feb 13, 2004
Loading...

Share This Page