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

M

Mike

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]
}
 
Q

Québec

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


Jean Pierre
--

--
http://web.jeanpierredaviau.com
Mike said:
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]
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top