473,396 Members | 1,738 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,396 software developers and data experts.

ASP Won't Set Cookie created in JavaScript

I am reading and setting a cookie using JavaScript in the BODY onload
and onunload events respectively. This works fine.

However when I use ASP to set the cookie under some condition where I
want to override the last value set in the onunload event, it has no
effect. The next onload still has the old value.

Any ideas? The following code simulates the problem I experience.

--------------------------------------------------
Filename = JSCookie.asp
--------------------------------------------------

<%@ Language=VBScript %>

<%
Option Explicit

%>

<script LANGUAGE="JavaScript">

var Value;

function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value)
+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}

function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) { // if there are any cookies
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset)
// set index of end of cookie value
if (end == -1)
end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
}
}

function Read()
{
Value = getCookie("Value");

// default to 1 if no cookie set yet (i.e. 1st time through)
if (!Value)
Value = 1;

alert("Read Value = " + Value);
}

function Change(NewValue)
{
Value = NewValue;
}

function Write()
{
setCookie("Value", Value, null);
alert("Wrote Value = " + Value);
}

</script>

<% Response.Cookies("Value") = 1 %>

<HTML>
<HEAD>
</HEAD>
<BODY onload="Read();" onunload="Write();">

<P><a href="javascript:Change(1);">Set Value to 1</a></P>
<P><a href="javascript:Change(2);">Set Value to 2</a></P>
<BR>
<P><a href="JSCookie.asp">Click Here to Reload Page</A>

</BODY>
</HTML>

--------------------------------------------------

Thanks,
Michael Levy
Jul 19 '05 #1
2 5526
Michael,

Surely cookies at the server are different than the ones at the client. The
javascript will only operate on cookies at the client and therefore will not
be the same.

HTH
Martin
"Michael" <Mi*****@MichaelLevy.net> wrote in message
news:44**************************@posting.google.c om...
I am reading and setting a cookie using JavaScript in the BODY onload
and onunload events respectively. This works fine.

However when I use ASP to set the cookie under some condition where I
want to override the last value set in the onunload event, it has no
effect. The next onload still has the old value.

Any ideas? The following code simulates the problem I experience.

--------------------------------------------------
Filename = JSCookie.asp
--------------------------------------------------

<%@ Language=VBScript %>

<%
Option Explicit

%>

<script LANGUAGE="JavaScript">

var Value;

function setCookie(name, value, expire) {
document.cookie = name + "=" + escape(value)
+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}

function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) { // if there are any cookies
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset)
// set index of end of cookie value
if (end == -1)
end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
}
}

function Read()
{
Value = getCookie("Value");

// default to 1 if no cookie set yet (i.e. 1st time through)
if (!Value)
Value = 1;

alert("Read Value = " + Value);
}

function Change(NewValue)
{
Value = NewValue;
}

function Write()
{
setCookie("Value", Value, null);
alert("Wrote Value = " + Value);
}

</script>

<% Response.Cookies("Value") = 1 %>

<HTML>
<HEAD>
</HEAD>
<BODY onload="Read();" onunload="Write();">

<P><a href="javascript:Change(1);">Set Value to 1</a></P>
<P><a href="javascript:Change(2);">Set Value to 2</a></P>
<BR>
<P><a href="JSCookie.asp">Click Here to Reload Page</A>

</BODY>
</HTML>

--------------------------------------------------

Thanks,
Michael Levy

Jul 19 '05 #2

The whole point of Cookies is to transfer information between the client
and server. So the cookies on the client are sent back to the server
with each HTTP request and the server sends them back in the response.
They should be the same.

Here is some additional debugging and what I've learned...

I changed a line in the ASP page to do the following:
Response.Cookies("Value") = Request.Cookies("Value")
I also added a line at the beginning of my Read() function:
alert(document.cookies);

The first time through, this new alert displayed "Value;" which makes
sense since ASP tried to read a non-existent cookie and wrote out a null
value. The JavaScript then tried getCookie("Value") which fails because
it is looking for "Value=" in the document.cookies string. A JavaScript
variable then gets set to 1.

I click the link to reload the web page. On the onunload event, the
JavaScript then writes out a cookie "Value=1;"

Now the ASP again tries to read and write the cookie. I'm not quite sure
what happens here.

The page is reloaded and the onload causes my alert to display
document.cookies which this time is "Value=1; Value"

The call to getCookie("Value") returns 1. So I know JavaScript is
reading and writing its cookie correctly.

Again I click the link to reload the page. ASP tries to read and write
the cookie.

When the page is reloaded, the onload event causes my alert to display
document.cookies as "Value=1; Value=1;". This leads me to believe that
ASP is reading the cookie written by JavaScript but is writing to a
different cookie that appears second in the document.cookies string. But
it also appears one iteration later then when the cookie is set by
JavaScript.

This has me very baffled.

HELP!!

Thanks,
Michael



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 19 '05 #3

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

Similar topics

5
by: Carl | last post by:
Hi, I want to pass the value of the cookie created in VB to ASP. This doesn't work. Any idea? Thanks Carl .... <script language=vbscript> document.cookie="mycookie" '...
4
by: Shannon Jacobs | last post by:
I'm doing some trivial surveys, and I want to know if the same user answers twice. Can't really know that, but at least I thought I could check for the same browser/computer combination by using a...
1
by: Display Name | last post by:
Used one of these canned scripts to set up a JS quiz but not before having used another canned PHP script for "Tell your friend about this Web page!" sort of thing. Now i've gotta integrate them;...
1
by: FayeC | last post by:
I got this snippet online and for the life of me I can't seem to make it write the cookie.... The Flash has the labels mentioned in the code and I used the name=movie and the swconnect=true as...
14
by: Schoo | last post by:
I have an asp.net app that uses session objects (ag. session("UserID")). The app works fine in development/debug mode. I released it to the test server (Windows 2000 server with other .NET...
15
by: Oleg Leikin | last post by:
Hi, (newbie question) I've created some simple .NET ASP application that should store cookies at the client machine. According to the documentation cookie expiration time is set via...
3
by: Wysiwyg | last post by:
After a server created cookie is processed on the client I want it removed, cleared, or expired in the javascript block but have been unable to do this. If I set a cookie value in the server code...
3
by: rjoseph | last post by:
Hi Guys I hope this is an easy one for you. Basically I have a page on my website that creates a javascript based cookie on the users pc. The contents of the cookie when created looks as...
3
by: TimSki | last post by:
Hi, I have the following javascript function which creates a cookie function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...
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.