Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Anyone have code for setting/getting cookies with cookie/key/value format?

Reply
Thread Tools

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

 
 
Mike
Guest
Posts: n/a
 
      09-04-2004
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[i] = a[i].trim()
if (a[i].substring(0,name.length) == name)
str = a[i].substring(name.length+1)
}
var a = str.split("&")
var obj = new Object()
if (str.length)
for (i=0; i<a.length; i++) {
temp = a[i].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]
}
 
Reply With Quote
 
 
 
 
Québec
Guest
Posts: n/a
 
      09-04-2004
http://www.webreference.com/javascri...125/index.html


Jean Pierre
--

--
http://web.jeanpierredaviau.com
"Mike" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed) om...
> 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[i] = a[i].trim()
> if (a[i].substring(0,name.length) == name)
> str = a[i].substring(name.length+1)
> }
> var a = str.split("&")
> var obj = new Object()
> if (str.length)
> for (i=0; i<a.length; i++) {
> temp = a[i].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]
> }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Give Request.Cookies and Response.Cookies is there any reason to use another method to use cookies? _Who ASP .Net 7 09-18-2008 07:49 PM
What's the proper way of reading cookies? Request.Cookies("mycook") doesnt work user ASP .Net 3 03-31-2007 01:53 PM
Response.Cookies vs Request.Cookies Alex Nitulescu ASP .Net 1 02-03-2005 09:43 AM
Persistent Cookies vs. session cookies Andy Fish Java 3 11-06-2003 10:44 AM
Good Cookies bad Cookies? AK Computer Support 23 10-26-2003 05:20 AM



Advertisments