View Full Version : Cookie problems

05-23-2006, 12:55 PM

I've been set this exercise that requires a web page to save text fields for login, password and save a checked tickbox. I've been fiddling around with it for hours on end, but no luck so far with any of it. I've looked on the internet, but the coding is a but different to what I've been using and I'm not quite sure what's going on. Here is what I've tried to do so far:

function addCookie(tag, value) {
var expireDate = new Date();
var expireString = "";
expireDate.setTime(expireDate.getTime() + (1000 * 60 * 60 * 24 * 365));
expireString = "expires=" + expireDate.toGMTString();
document.cookie = tag + "=" + escape(value) + ";" + expireString + ";";

function getCookie(tag) {
var value = null;
var myCookie = document.cookie + ";";
var findTag = tag + "=";
var endPos;

if (myCookie.length > 0) {
var beginPos = myCookie.indexOf(findTag);
if (beginPos != -1) {
beginPos += findTag.length;
endPos = myCookie.indexOf(";", beginPos);
if (endPos == -1)
endPos = myCookie.length;
value = unescape(myCookie.substring(beginPos, endPos));
return value;

function saveLoginInfo() {
if (document.CustomerLogin.ID.value != null) {
addCookie("ID", document.CustomerLogin.ID.value)

if (document.CustomerLogin.ID.value != null) {
addCookie("Password", document.CustomerLogin.Password.value)

addCookie("SavePassword", document.CustomerLogin.SavePassword.value)


function getLoginInfo() {
var ID = getCookie("ID");

var Password = getCookie("Password");

var SavePassword = getCookie("SavePassword");
(document.CustomerLogin.SavePassword.checked == true)
else (document.CustomerLogin.SavePassword.checked == false)



We were given the 2 'generic functions' (addCookie() and getCookie()) that I tried to call on in the saveLoginInfo() and getLoginInfo() functions. I've placed saveLoginInfo() into the FORM tag using onSubmit, whilst I put getLoginInfo() into the BODY tag with the onLoad event handler.

We were told that the saveLoginInfo() function is supposed to:
- Save the login (named "ID") and password ("Password") fields if the save password checkbox is checked ("SavePassword").
- Save the value of the checkbox

getLoginInfo() is supposed to look for cookies saved, and then automatically repopulate the values on the form.

For the saveLoginInfo function, I've tried to write the code that validates the information entered by the user in the login and password field, and the added the cookie. With getLoginInfo() I'm even more lost. I'm trying to tick the checkbox if getCookie recalls a value, but I'm pretty sure I'm way off track in what I've done. :(

Any help would be much appreciated! Thanks!