473,721 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=VBScri pt %>

<%
Option Explicit

%>

<script LANGUAGE="JavaS cript">

var Value;

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

function getCookie(Name) {
var search = Name + "="
if (document.cooki e.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(docume nt.cookie.subst ring(offset, end))
}
}
}

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

// 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("Valu e", Value, null);
alert("Wrote Value = " + Value);
}

</script>

<% Response.Cookie s("Value") = 1 %>

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

<P><a href="javascrip t:Change(1);">S et Value to 1</a></P>
<P><a href="javascrip t:Change(2);">S et 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 5579
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*****@Michae lLevy.net> wrote in message
news:44******** *************** ***@posting.goo gle.com...
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=VBScri pt %>

<%
Option Explicit

%>

<script LANGUAGE="JavaS cript">

var Value;

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

function getCookie(Name) {
var search = Name + "="
if (document.cooki e.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(docume nt.cookie.subst ring(offset, end))
}
}
}

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

// 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("Valu e", Value, null);
alert("Wrote Value = " + Value);
}

</script>

<% Response.Cookie s("Value") = 1 %>

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

<P><a href="javascrip t:Change(1);">S et Value to 1</a></P>
<P><a href="javascrip t:Change(2);">S et 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.Cookie s("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("Valu e") which fails because
it is looking for "Value=" in the document.cookie s 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.cookie s which this time is "Value=1; Value"

The call to getCookie("Valu e") 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.cookie s 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.cookie s 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
2088
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" ' also tried with document.cookie="name=mycookie"
4
5294
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 cookie. Here is the code I have now. In the header, I have the following: <SCRIPT language="JavaScript"> var cookieStatus; if (document.cookie.length > 0) { cookieStatus = 'Cookie exists with value ' + document.cookie; } else {
1
2464
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; client wants the results from the quiz to go into the email. The file "quizconfig.js" parses the form-submitted test results & the test results page (quiz1_results.htm), in turn, shows the score. The JS in the quizconfig.js appears to set a...
1
1599
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 mentioned in the comments. The movie is fine.....the code is just not writing the cookie at all... Could someone take a look and see if you can figure out why it won't work? Thanks, FayeC
14
3381
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 applications running on it) and when I am sitting at that server running the application, it also runs fine. But, if I sit at any workstation on the LAN it does not work. I narrowed the problem down to the fact that all of the session objects contain...
15
3649
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 HttpCookie.Expires property, but property value is the time of day on the client. How can I possibly know client local time ?
3
11120
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 behind and don't use a domain then I can not change or remove that cookie's value on the client. If I subsequently create the cookie again in the codebehind then I actually end up with TWO cookies with the same name in the response. The cookie...
3
1781
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 follows: ------------------------------ DS1113008PT_ToDoItem1 My%20message%20goes%20here www.mydomain.co.uk/carsearch/usedcars/
3
2076
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); document.cookie=c_name+ "=" +escape(value) +((expiredays==null) ? "" : ";expires=" +exdate.toGMTString());
1
9130
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9059
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8005
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6668
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5977
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4751
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3187
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
3
2129
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.