473,856 Members | 1,745 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 5601
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
2093
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
5303
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
2471
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
1605
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
3397
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
3661
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
11131
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
1792
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
2085
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());
0
9908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9760
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11052
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10695
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10777
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
10379
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
5759
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...
2
4172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3198
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.