472,341 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,341 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 5368
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>...
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...
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...
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...
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...
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...
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...
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...
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();...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.