Click to See Complete Forum and Search --> : Adding a cookie's value to itself...


dmason165
04-28-2003, 03:19 AM
Hello everyone,

I don't even know if this is possible, but don't see why it wouldn't be.

My question is very simple to word and I hope an answer comes just as easily.

How do you add the value retained in a cookie to an equation within the same cookie?

i.e.
TimeIn = new Date();

function SetCookie() {
var TimeOut = new Date();
var today = new Date();
var expire = new Date();
var nDays=2;
var cookieName="SomeValue";
var TimeSpent=(TimeOut - TimeIn);
var TimeRemaining=(SomeValue + TimeSpent);
var cookieValue=TimeRemaining;
expire.setTime(today.getTime() + 600000);
document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}

The variable TimeRemaining would become the value of the cookie's previous value plus (+) the value of the variable TimeSpent.

Of course, the first time the cookie is set, the value of "SomeValue" in the equation would be zero (0).

Thanks!

~D

khalidali63
04-28-2003, 07:10 AM
Provided that I understood your question correctly...:D .

Cookies can not keep the state of a value,they can merely act as a storage element in web pages,to change a value in the cookie you just have re-write it,It does not have methods like previous or next...:-)
Take a look at this link..the cookie keeps track of how many time a user has visited the site...its the exact same idea you are inquiring about..

http://68.145.35.86/skills/javascripts/CompleteCookieUseage.html

dmason165
04-28-2003, 07:31 AM
Khalidali,

Thanks for the reply. I looke to the source of the reference you provided but I was unable to extract a solution to my problem. Perhaps you could help me.

How can I add the value of a cookie to an equation?

~D

khalidali63
04-28-2003, 07:37 AM
Take a look at the code again...you will see that at first visit the counter is =0,
the second time,I get the value from teh cookie and increase it for every subsequent visit to that page.
to anything with any equation you will follow the same logic and you can get it done,I hope you are not asking towrite the code for you..:D

dmason165
04-28-2003, 08:23 AM
Now I have this for setting the cookie:

TimeIn = new Date();

function SetCookie() {
var TimeOut = new Date();
var today = new Date();
var expire = new Date();
var nDays=2;
var cookieName="TimeRemaining";
var TimeSpent=(TimeOut - TimeIn);
var TimeRemaining+=TimeSpent;
var cookieValue=TimeRemaining;
expire.setTime(today.getTime() + 600000);
document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}

However, it returns an error that reads, "Expected ';'" for the line in red which is the line I have changed.

Why is it returning an error?

~D

khalidali63
04-28-2003, 08:27 AM
I think that is because you do not have the TimeRemaining variable initialised,..try this

var TimeRemaining = 0;
TimeRemaining +=TimeSpent;

dmason165
04-28-2003, 08:28 AM
Thank you Khalidali...I'll try that.

~D

dmason165
04-28-2003, 08:33 AM
I'm still getting the same error.

My code now looks like this:

TimeIn = new Date();

function SetCookie() {
var TimeRemaining = 0
var TimeOut = new Date();
var today = new Date();
var expire = new Date();
var nDays=2;
var cookieName="TimeRemaining";
var TimeSpent=(TimeOut - TimeIn);
var TimeRemaining+=TimeSpent;
var cookieValue=TimeRemaining;
expire.setTime(today.getTime() + 600000);
document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}

What's wrong:confused:

~D

khalidali63
04-28-2003, 08:38 AM
Now you ae defining
var TimeRemaining = 0
twice..:-)
at the top and at here

var TimeRemaining+=TimeSpent;

change the line above
TimeRemaining+=TimeSpent;

dmason165
04-28-2003, 08:43 AM
Thank You!! Thank You!! Thank You!! Thank You!! Thank You!! Thank You!! Thank You!! Thank You!! Thank You!!

I got a positive test result with that run!

Thanks again!

~D

khalidali63
04-28-2003, 08:45 AM
:D
Now with that kinda apreciation..you made my day..:-)

dmason165
04-28-2003, 08:49 AM
My almost-working code is this:

TimeIn = new Date();

function SetCookie() {
var TimeRemaining = 0
var TimeOut = new Date();
var today = new Date();
var expire = new Date();
var nDays=2;
var cookieName="TimeRemaining";
var TimeSpent=(TimeOut - TimeIn);
var cookieValue=TimeRemaining;
TimeRemaining+=TimeSpent;
expire.setTime(today.getTime() + 600000);
document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}

I use an alert to give me the value of the cookie when it is set. However, each time I set the cookie, the value should be increasing because the amount of time should be added to the value before...right?

It's not increasing at all....it's just giving me the time spent at that one particular session.

~D

dmason165
04-28-2003, 08:50 AM
Khalidali,

I'm very appreciative;)

~D

khalidali63
04-28-2003, 08:53 AM
Thats because your cookie value is not being updated correctly,here use this

TimeIn = new Date();

function SetCookie() {
var TimeRemaining = 0
var TimeOut = new Date();
var today = new Date();
var expire = new Date();
var nDays=2;
var cookieName="TimeRemaining";
var TimeSpent=(TimeOut - TimeIn);
TimeRemaining+=TimeSpent;
var cookieValue=TimeRemaining;
alert(cookieValue);
expire.setTime(today.getTime() + 600000);
//document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}


once everything is validated then uncomment the line that sets the cookie,so that it can se it...:-)

dmason165
04-28-2003, 08:58 AM
Ok....I will try that.

Thank you so very much for your time, patience and help!

~D

dmason165
04-28-2003, 09:08 AM
Khalidali,

The value still does not increase. It should be right? The code looks good to me.

Help.

~D

khalidali63
04-28-2003, 09:15 AM
I'd tested the code before I posted,and it increases the value...you might have some other problems in the code..:-)

khalidali63
04-28-2003, 09:19 AM
Here is the temp page I worked on...

http://68.145.35.86/temp/dmason165-cookieError.html

dmason165
04-28-2003, 09:20 AM
I don't know...we should be testing the exact same code. What could possibly be keeping it from doing what it's supposed to??

I'm testing:

TimeIn = new Date();

function SetCookie() {
var TimeRemaining = 0
var TimeOut = new Date();
var today = new Date();
var expire = new Date();
var nDays=2;
var cookieName="TimeRemaining";
var TimeSpent=(TimeOut - TimeIn);
TimeRemaining+=TimeSpent;
var cookieValue=TimeRemaining;
alert(cookieValue);
expire.setTime(today.getTime() + 600000);
document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}


Thank you for all your help!

~D

dmason165
04-28-2003, 09:30 AM
That's really cool...that's exactly what I want!! I'm going to look at your source in a min.

But, would this work if I left the page and came back to it?

In other words, if I spent 5 seconds on it, left the page, and then returned to it later and spent 5 more seconds, would the value come back as 10000?

~D

dmason165
04-28-2003, 09:36 AM
Ok...I have tested my code in the same manner that you have...without leaving the page...and it works just as yours does:)

Now, the question is, how do I make it do the same thing even if the user leaves the page?...that's the only purpose for this cookie and it IS supposed to retain that value....right? I thought that was the whole purpose of these complicated things we call cookies:D ;)

Am I asking for too much now?

~D

khalidali63
04-28-2003, 09:43 AM
Thats the second part of the cookie structure..

1. read cookie
2. if does not exist write it
3. write cookie,
now you need to write the cookie.

you should look at the code in the url I first posted..

http://68.145.35.86/skills/javascripts/CompleteCookieUseage.html

dmason165
04-28-2003, 10:03 AM
I have the code to read the cookie, but all that does is get the value of the cookie right? What I mean is, if I'm testing the cookie, and the first value the test gives me is 5000 (or 5 seconds) and then I leave and go back to the test and get a second value of 5000....then it's obvious the cookie is not adding the previous value to the current one and thus, would only give a value of 5000 to the function getCookie() instead of 10000...which is the total amount of time I spent on the test page.

I hope that makes sense. If not just let me know.

Thanks!

~D

khalidali63
04-28-2003, 10:09 AM
how about postin gall of your cookie related code..?

dmason165
04-28-2003, 10:12 AM
Khalidali,

Actually, now that I think about it, YOUR code is a perfect example of what I'm trying to say. Your code increments 1 each time a viewer returns to your page. So they are obviously leaving and coming back and the value is still incremented. The only difference between your code and mine is that I don't report the value to the viewer.

Why does yours do what I want mine to do, but mine does not?

~D

khalidali63
04-28-2003, 10:18 AM
Finally....lol...(jking)you are almost there,just post your all of the code and I'll take a look...it must be some tiny hickup....

dmason165
04-28-2003, 10:36 AM
Sorry for taking so long...my CPU started acting funny and I had to restart it..

Anyhow, here's my comlete cookie code:

<html>
<head>
<title>Timed Page Cookie TEST</title>
<script language="JavaScript" type="text/javascript">
<!--
TimeIn = new Date();

function SetCookie() {
var TimeRemaining = 0;
var TimeOut = new Date();
var today = new Date();
var expire = new Date();
var nDays=2;
var cookieName="TimeRemaining";
var TimeSpent=(TimeOut - TimeIn);
TimeRemaining +=TimeSpent;
var cookieValue=TimeRemaining;
alert(cookieValue);
expire.setTime(today.getTime() + 600000);
document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}

function GetCookie() {
var NameOfCookie="TimeRemaining";
if(document.cookie.length > 0)
{
begin = document.cookie.indexOf(NameOfCookie+"=");
if(begin != -1)
{
begin += NameOfCookie.length + 1;
end = document.cookie.indexOf(";",begin);
if(end == -1) end = document.cookie.length;
TimeSpent=(document.cookie.substring(begin,end));
}
}
}

//function that checks viewer's reading pace
function CheckSpeed() {
SetCookie();
GetCookie(TimeRemaining);
if (TimeSpent < 180000 ){
alert("You're reading awfully fast. Don't you think you should take it a bit more slowly?");
return false;
}
else {
return true;
}
}
//-->
</script>
</head>

<body>
This link takes the viewer to a <a href="Notes.htm" onClick="SetCookie()">reference page</a> and is not restricted by the time defined in CheckSpeed()<br>
This link goes to the next page and is timed. <a href="NextPageTEST.htm" onClick="CheckSpeed()">Next
Page</a>
</body>
</html>

Thanks again for all your help Khalidali!

~D

khalidali63
04-28-2003, 10:42 AM
Ok here is what you missed so far...
you are not calling GetCookie() anywhere,which logically should be called when page loads.Just replace your body tag
<body>

with this
<body onload="GetCookie();">

and you are all set...

dmason165
04-28-2003, 10:56 AM
Hi Khalidalim

I call the GetCookie function under the function CheckSpeed.

Under the CheckSpeed function, I set the cookie first just in-case the viewer doesn't follow any of the other links on the page that are not restricted by the CheckSpeed function.

~D

khalidali63
04-28-2003, 11:24 AM
Here you go

http://68.145.35.86/temp/dmason165-cookieError.html

Check it out

dmason165
04-28-2003, 11:38 AM
Khalidali,

Thank you for all your help...I can't believe you stuck with me this long:D

I'm going to test the code you provided and see if it works.

I tried it on the page you made, but when the the "next page" link is clicked, it gives the alert, but still allows advancement to the next page...don't know why yet. I'm also going to throw in an alert for the value of the cookie so I know it's adding the time properly.

So, I may not post for about 20 minutes or so. But I will and hopefully the post will be full of good news!!

Again, THANK YOU!

~D