In article <11**********************@z14g2000cwz.googlegroups .com>,
so**********@yahoo.com wrote:
I'm trying to store a value in a cookie but its not working. Can
anyone see what I might be doing wrong.
Here is a copy of my earlier post. Pick a working cookie routine.
Robert
Here are my suggestions.
1) Always set the expiration time. IE works better.
2) You may have disabled cookies. In IE, I had to go to the site then
check whether or not cookies were enabled.
3) Maybe your cookie rountines could use improvements.
I suggest using an existing set of cookie routines. The
document.cookie can contain existing cookies and you need to preserve
them. ( Don't know why such routines aren't shipped with the browsers.
)
~kaeli~ has developed some cookie code. You may view it in this
article:
http://groups.google.com/groups?hl=e...SO-8859-1&selm...
..1af96c2a4f8abc94989dcc%40nntp.lucent.com
Also, ~kaeli~ reports that:
There's some even better code that sets more cookie properties like
domain, secure, path, etc here.
http://www.acm.uiuc.edu/webmonkeys/j...ipt/cookies.js
4) I wrote a short program for verifying cookies will work on a site.
The cookie code was taken from:
Functions from Using HTML 4, Java 1.1, and JavaScript 1.2:
Complete. Definitive. Invaluble.
by Eric Jim O'Donnell, et al.
and from the Netscape develper's site. Here is my cookie testing
code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>Remember by cookie</title>
<script type="text/javascript">
// Sets cookie values. Expiration date is optional
//
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 deleteCookie(name,path,domain) {
if (getCookie(name)) document.cookie = name + "=" +
( (path) ? ";path=" + path : "") +
( (domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-70 00:00:01 GMT";
}
function registerCookie(cookieName,cookieValue) {
var today = new Date()
var expires = new Date()
expires.setTime(today.getTime() + 1000*60*60*24*365)
setCookie(cookieName, cookieValue, expires)
}
</script>
<body>
<p>Let's see if we can set then read a cookie
<script type="text/javascript">
document.write("when on (document.url) " + document.URL);
document.write("</p>");
var all = "I hope this cookie doesn't crumble.";
document.write("<p>");
document.write("document.cookie before deleteCookie = "
+ document.cookie + "<br><br>");
deleteCookie("mycookietry");
document.write("document.cookie after deleteCookie= "
+ document.cookie + "<br><br>");
registerCookie("mycookietry",all);
document.write("document.cookie after registerCookie = "
+ document.cookie + "<br><br>");
theMessage = getCookie("mycookietry");
document.write("theMessage = " + theMessage + "<br>");
document.write("</p>");
</SCRIPT>
</body>
</html>