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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with disappearing cookie

Status
Not open for further replies.

maynard444

Programmer
Jul 1, 2003
13
0
0
US
Howdy neighbors,

I need some assistance with some missing cookies. At first I thought the computer was just eating them, but then I woke up. Ok first I will lay out what i am working with. I start out with an index document. It has several different links on it that call functions to set a cookie to load unique information to a generic form(stuff like the header text, links to forms and publications, and so on). The second page that is called when the link on the first page is clicked contains all the correct information that the cookie passed. On this form there are also links, when these are pressed they do basically the same thing the links on the first page do. They set a cookie to insert data into the next form. When a link is clicked it loads all the information in the third document correctly. The problem I have is that when I hit the back button to go to the second page to select another option, the data pulled from the cookie read on the first page is gone. I need to know if anyone has a remedy for this. I've had about 7 different projects thrown on me in the last 2 weeks and I am in a crunch to get these done because early September I will most likely be beginning an Implementation of JD Edwards HRIS. Any info anyone could give would help. Thanks,
Heath
Oh yeah, here is a little snippet of some of the code i use to read and write the cookies:
This is in the head of the first page-
function regioncode() {
var now = new Date();
now.setMinutes(now.getMinutes() + 10);
var regiondata =escape("Louisville");
document.cookie=regiondata;
window.location="louisville_home_page.htm";
}
FYI Timing out because of the expiration time is not the problem.
In the body i call the function with an onClick.

This is in the head of the second page-
function autofill1() {
var now = new Date();
now.setSeconds(now.getSeconds() + 3);
var state= escape('KY');
var email = escape("somebody.somehow@cracky.com");
document.cookie = state + "xxx" + email + "xxx" + ";expires=" +now;
window.location = "address_change.htm";
}

var readcookie=unescape(document.cookie);
var title=readcookie.toString();this part is used to write part of a title in the body.
As you can see, I am also reading a cookie in ths page also to gather specific in formation for the third page. If anybody knows how to recall a cookie, or how to call a function to read a new cookie on the back() event I would be in your debt.
 
I have a thought. Why have the cookie expire now rather than set the expiration 1 day from now? As long as the cookie doesn't expire then you can still get the value from it. Have the page check for the cookie when it first loads with a conditional statement (if its there retrieve it, else create it) Then to remove the cookies on your last page reset the values of all the cookies to change the expiration to now with a submit button. I was just thinking that if the user hit the back button they felt that they may have made a mistake and might want to change something before moving on..


var expireDate = new Date();
nowDate.setDate(nowDate.getDay() + 1;
setCookie("name1", "value1", "path1", expireDate.toGMTString());


to change the (name1) cookies value like this...

var expireDate = new Date();
nowDate.setDate(nowDate.getDay() + 1;
setCookie("name1", "value99", "path1", expireDate.toGMTString());

you can do the same to change the expiration date

var expireDate = new Date();
nowDate.setDate(nowDate.getDay() - 1;
setCookie("name1", "", "", expireDate.toGMTString());

Here is a snippet from "Wrox" to retrieve values from a cookie..

function getCookieValue(cookieName)
{
var cookieValue = document.cookie;
var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
if (cookieStartsAt == -1)
{
cookieStartsAt = cookieValue.indexOf(cookieName + "=");
}
if (cookieStartsAt == -1)
{
cookieValue = null;
}
else
{
cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
if (cookieEndsAt == -1)
{
cookieEndsAt = cookieValue.length;
}
cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
}
return cookieValue;
}


Hope this helps you out and that I haven't overstepped my boundaries with any misunderstanding.

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top