Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading/Writing Cookies: ASP & JavaScript

Status
Not open for further replies.

gacaccia

Technical User
May 15, 2002
258
US
I've recently noticed some issues with using cookies between ASP and Javascript. Anyone aware of any specific problems? Basically what I'm running into is that I set a cookie for a login value using ASP. I want to have a logout link and was trying to set the cookie value using Javascript. This appears to work. However, if I try to login again, the cookie value setting in ASP is not "sticking" and the cookie retains a logout value!?

As a test, I created an ASP page where session and saved cookie value are set using both ASP and Javascript. This text page exhibits the same kind of strange behavior and I can't make any sense of it. If you have the time, please take a look.

Here's the code for the test page...

Code:
<%@ LANGUAGE=VBSCRIPT %>
<% Option Explicit %>
<% Response.Buffer = True %>
<html><head><title>Test Cookies</title>
<script src=&quot;includes/cookies.js&quot; language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;></script>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
<!--
var expDate = new Date();
expDate.setTime (expDate.getTime() + (10 * 60 * 1000));
//-->
</script>
</head>
<body>
<%
if request.cookies(&quot;session&quot;) = &quot;&quot; then
	 response.cookies(&quot;session&quot;) = &quot;true&quot;
else
	 response.write &quot;<p>Session cookie = &quot; & request.cookies(&quot;session&quot;)
	 response.cookies(&quot;session&quot;) = &quot;true&quot;
	 response.write &quot;<br>After ASP cookie assignment, session cookie = &quot; & request.cookies(&quot;session&quot;) & &quot;</p>&quot;
end if
if request.cookies(&quot;saved&quot;) = &quot;&quot; then
	 response.cookies(&quot;saved&quot;) = &quot;true&quot;
	 response.cookies(&quot;saved&quot;).expires = date() + 1
else
	 response.write &quot;<p>Saved cookie = &quot; & request.cookies(&quot;saved&quot;)
	 response.cookies(&quot;saved&quot;) = &quot;true&quot;
	 response.cookies(&quot;saved&quot;).expires = date() + 1
	 response.write &quot;<br>After ASP cookie assignment, saved cookie = &quot; & request.cookies(&quot;saved&quot;) & &quot;</p>&quot;
end if
%>
</p>
<a href=&quot;javascript:setCookie('session','false',null)&quot;>Set Session Cookie to False</a>
<br><a href=&quot;javascript:setCookie('saved','false',expDate)&quot;>Set Saved Cookie to False</a>
<form method=&quot;post&quot; action=&quot;testCookies.asp&quot; name=&quot;frm&quot;>
<input type=&quot;submit&quot; name=&quot;btn&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>

Also, here is the source file for cookies.js...

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

function setExpiration(days) 
{
  var expires = new Date();
  var today = new Date();
  expires.setTime (today.getTime() + (1000*60^60^24*parseInt(days)));
  return expires;
}

Thanks in advance for any help or ideas.

Glenn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top