473,516 Members | 3,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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]
}
Jul 23 '05 #1
1 3231
http://www.webreference.com/javascri...125/index.html
Jean Pierre
--

--
http://web.jeanpierredaviau.com
"Mike" <mi*************@yahoo.com> a écrit dans le message de
news:6a**************************@posting.google.c 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]
}

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1856
by: terry | last post by:
Hi, When I store cookie value "12:05:33" and read it again, I find the value become "12%3A05%3A33", how to read or convert it correctly? Thanks!
1
2821
by: Julius Fenata | last post by:
Dear all, I need help to change my item-template value format... Here is my case, I have a datagrid, with 'SubjectPrice' field, and when the grid displayed, my 'SubjectPrice' field displayed as this format: $30,000.00, because I put it this format {0:C}. I need to re-format as only this value (example: 30.000 - without currency,...
0
1223
by: Kevin | last post by:
Hello, all experts: May I ask you some questions? First, i'm programming a software of auto-authentication by using VB.NET tool. This software's functions is similar to do the account login through browser. I meet some questions when I post data to authentication page. I have tested the authentication webpages of Gemtek G-6000 and...
0
1817
by: JT | last post by:
I posted a question earlier about communicating between a javascript function and vb.net. In the end I decided to try using a cookie. I use the following javascript function (from vb.net) to set the cookie: sb.Append("<script language='javascript'>") sb.Append("{if (confirm('Are you sure? ') == true)") sb.Append("{var date = new Date();...
5
2381
by: Zeba | last post by:
Hi ! I want to know the cookie value that was set in the previous page. Initially in my page in project B I used HttpCookie cookie = Request.Cookies.Get(somefile.COOKIE_NAME); where cookie name is set using web.config. As a result when I call this page of project B from some page in Project A, I get the cookie
3
2565
by: korque | last post by:
Hi I'm gathering information from web page, first I get headers and store recieved cookies then I get source with file()-function. Is there easy way to send cookie value back to host on each page, so my script would be recognized as web browser and sessid wouldn't change on every page ?
13
2061
by: jcato77 | last post by:
I am having trouble figuring out my code and was hoping someone could point me in the right direction. Below is my code what I need to due is create a method to add and display the value of the entire inventory. I have what I think is the correct code but it's not working. Help is greatly appreciated class Inventory3 { public static...
3
2582
by: webster5u | last post by:
Miss/Mr. Programmer, I have a idea to remove the cookie value from a website after all browser closed. It is like session kept in server and eliminate after all browser closed. My purpose is reducing browsing security issue through minimize the data resident in the cookie. The reason i using the cookie instead of session because I used...
2
2504
by: ramuksasi | last post by:
Hi, I am unable to read a cookie value from the cookies. i have set cookie value from javascript in scripting side and try to read cookie value from coding part ie. default.aspx.vb. i have shown my javascript page below: <pre lang="cs">function setcookie() { var today=new Date(); var expire=new Date(); var ndays; ...
0
7273
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7574
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5106
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1620
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.