Click to See Complete Forum and Search --> : Problems writing a simple coookie


craigcsbdon
09-03-2003, 10:43 AM
Hi,
Im having problems with a simple problem.
I'm trying to detect whether users have cookies and Javascript enabled from this tutorial on 4 Guys from Rolla ( http://www.aspfaqs.com/webtech/082400-1.shtml ). I have one page which writes a cookie using Javascript then when the body loads it detects the cookie, if the cookie exists it changes the value of a hidden field in a form. The form should then submit to another page which looks at the value of the hidden field, if it hasnt been changed then either cookies or javascript is disabled or not supported. I cant get it work. Im using internet explorer with javascript and cookies enabled but i cant get the javascript to write the cookie. Im sure its something simple. I was really appreciate it if anyone could point out any obvious reasons why it wouldnt work.

Heres the code for the page with the hidden field and the javascript function to change the hidden field.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<html>
<head>
<script language="JavaScript">
<!-
function cc()
{
/* check for a cookie */
if (document.cookie == "")
{
/* if a cookie is not found - alert user -
change cookieexists field value to false */
alert("COOKIES need to be enabled!");

/* If the user has Cookies disabled an alert will let him know
that cookies need to be enabled to log on.*/

document.Form1.cookieexists.value ="false"
} else {
/* this sets the value to true and nothing else will happen,
the user will be able to log on*/
document.Form1.cookieexists.value ="true"
}
}

/* Set a cookie to be sure that one exists.
Note that this is outside the function*/
document.cookie = 'killme' + escape('nothing')
// -->
</script>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body onLoad="cc()">
<form action="check2.asp" method="post" name="Form1" id="Form1">
<input type="hidden" name="cookieexists" value="false"><input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>

Thanks for any advice
Craig

Khalid Ali
09-03-2003, 11:00 AM
here is a pretty simple cookie writing script example.Hope it helps.

http://www.webapplikations.com/pages/html_js/document/SimpleLoginCookieScript.html