By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

ASP Won't Set Cookie created in JavaScript

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.