Anyone have code for setting/getting cookies with cookie/key/value format?

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

  1. Mike

    Mike Guest

    I got the code below from an earlier post but I can't get it to work
    (I get an error on the "for (i=0; i<a.length; i++)" line) Anyone have
    code that works for cookies with keys?



    > Anyone got any code out there that will set and retrieve
    > cookie values that have the key/subKey structure?


    Using the code provided below, examine

    getCookie("APP","GROUPNUMBER")

    and

    addToCookie("APP","GROUPNUMBER",yourValue)



    String.prototype.trim = function(){return
    this.replace(/(^\s*)|(\s*$)/g, "")}
    function parseCookie(name) {
    var str = ""
    var a = document.cookie.split(";")
    for (var i=0; i<a.length; i++) {
    a = a.trim()
    if (a.substring(0,name.length) == name)
    str = a.substring(name.length+1)
    }
    var a = str.split("&")
    var obj = new Object()
    if (str.length)
    for (i=0; i<a.length; i++) {
    temp = a.split("=")
    obj[temp[0]] = temp[1]
    } return obj
    }
    function addToCookie(cookieName,key,value) {
    var cookies = parseCookie(cookieName)
    cookies[key] = value
    var d = new Date()
    d.setFullYear(d.getFullYear()+1)
    var cookieStr = cookieName + "="
    for (var item in cookies)
    cookieStr += item + "=" + cookies[item] + "&"
    cookieStr = cookieStr.substring(0,cookieStr.length-1)
    cookieStr = cookieStr + "; expires=" + d.toUTCString()
    cookieStr = cookieStr + "; path=/"
    document.cookie = cookieStr
    }
    function getCookie(name,key) {
    var cookies = parseCookie(name)
    return cookies[key]
    }
     
    Mike, Sep 4, 2004
    #1
    1. Advertising

  2. Mike

    Québec Guest

    http://www.webreference.com/javascript/961125/index.html


    Jean Pierre
    --

    --
    http://web.jeanpierredaviau.com
    "Mike" <> a écrit dans le message de
    news:...
    > I got the code below from an earlier post but I can't get it to work
    > (I get an error on the "for (i=0; i<a.length; i++)" line) Anyone have
    > code that works for cookies with keys?
    >
    >
    >
    > > Anyone got any code out there that will set and retrieve
    > > cookie values that have the key/subKey structure?

    >
    > Using the code provided below, examine
    >
    > getCookie("APP","GROUPNUMBER")
    >
    > and
    >
    > addToCookie("APP","GROUPNUMBER",yourValue)
    >
    >
    >
    > String.prototype.trim = function(){return
    > this.replace(/(^\s*)|(\s*$)/g, "")}
    > function parseCookie(name) {
    > var str = ""
    > var a = document.cookie.split(";")
    > for (var i=0; i<a.length; i++) {
    > a = a.trim()
    > if (a.substring(0,name.length) == name)
    > str = a.substring(name.length+1)
    > }
    > var a = str.split("&")
    > var obj = new Object()
    > if (str.length)
    > for (i=0; i<a.length; i++) {
    > temp = a.split("=")
    > obj[temp[0]] = temp[1]
    > } return obj
    > }
    > function addToCookie(cookieName,key,value) {
    > var cookies = parseCookie(cookieName)
    > cookies[key] = value
    > var d = new Date()
    > d.setFullYear(d.getFullYear()+1)
    > var cookieStr = cookieName + "="
    > for (var item in cookies)
    > cookieStr += item + "=" + cookies[item] + "&"
    > cookieStr = cookieStr.substring(0,cookieStr.length-1)
    > cookieStr = cookieStr + "; expires=" + d.toUTCString()
    > cookieStr = cookieStr + "; path=/"
    > document.cookie = cookieStr
    > }
    > function getCookie(name,key) {
    > var cookies = parseCookie(name)
    > return cookies[key]
    > }
     
    Québec, Sep 4, 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. _Who
    Replies:
    7
    Views:
    2,767
  2. Une bévue
    Replies:
    5
    Views:
    173
    Une bévue
    Aug 10, 2006
  3. Phil Powell
    Replies:
    2
    Views:
    235
    Grant Wagner
    Sep 5, 2003
  4. Timur Tabi
    Replies:
    1
    Views:
    140
    Timur Tabi
    May 14, 2004
  5. Jeff
    Replies:
    4
    Views:
    155
Loading...

Share This Page