473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
Jul 23 '05 #1
1 2190
In article <6a**************************@posting.google.com >,
mi*************@yahoo.com 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

Jul 23 '05 #2

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

Similar topics

15
by: Thomas Scheiderich | last post by:
I am trying to understand Session variables and ran into a question on how they work with data that is passed. I have an HTM file that calls an ASP file and sends the name either by GET or POST....
2
by: David Billson | last post by:
Hi all, Fairly straight forward problem. I have a Post using HttpWebRequest that posts data to a server. That server then posts data back to me. Posting the data's not a problem, however I'm...
4
by: James Johnson | last post by:
Dear C#Dex, I am trying to automate a POST to a web page that clicks a button. I have been able to hit a target web page and run the web page. However, the button on the page does not click. ...
5
by: Tony Pino | last post by:
Hi, Lets say there's a web site with simple authentication. It asks you to type a uname/password into a couple text boxes, and then it gives you a cookie and you're logged in for 20 minutes or...
0
by: Shapper | last post by:
Hello, I have this code in Global.asax: Sub Session_Start(Sender As Object, E As EventArgs) Dim cookie As HttpCookie = Request.Cookies("MyCookie") If Not cookie Is Nothing Then...
0
by: WIWA | last post by:
Hi, I want to login to a password protected website and fetch the content of the page behind. I have based my code on http://weblogs.asp.net/jdennany/archive/2005/04/23/403971.aspx. When I use...
3
by: David Davidson | last post by:
I'm just wondering if anybody's seen a problem with their web server recently that's similar to the following: about two weeks ago some clients on our intranet started experiencing lost sessions -...
6
by: test | last post by:
Hi everyone, I'm creating a desktop Python application that requires web-based authentication for accessing additional application features. HTTP GET is really simple. HTTP POST is not (at...
5
by: manheim | last post by:
I have a page posting a raw jpg to me via HTTP POST. All reference I've found is centered around retrieving POST data as name/value pairs which will not work in this situation. What I need to do...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.