Click to See Complete Forum and Search --> : Cookie request


dmason165
04-11-2003, 09:22 PM
At this point, the biggest cookie request I have will work with another solution I got from this forum.

I was given the JS which allowed me to put a timer on a page. It worked like this:

If the user tried to advance to the next page without taking the minimal amount of time to read the material, an alert notified them of their carlessness of the information and asked them to exit if they weren't going to take the time to read it.

Anyway, the pages that I want to apply this JS to contain links to the other pages (for reference purposes). So, in order for the timed page to work effectively, I need a cookie that will "keep track" of the time for each page.

The code looks something like this:

<html>
<head>
<title>Untitled</title>
<script language="Javascript" type="text/javascript">
<!--
firsttime = new Date();

function checkspeed() {
secondtime= new Date();
difference = secondtime - firsttime;
if (difference <= 60000 ){
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>

<a href="next.htm" onClick="return checkspeed()">leave</a>

</body>
</html>


i.e. If the user follows one of the reference links on the timed page, the length of time remaining is stored with a cookie. When the user returns to that timed page, the timer continues from where it left off.

So even if the user exits the presentation, when the user returns, however long that might be (before it expires of course - 1 week) , the timer will pick-up where it left-off.

To make things more complicated (maybe), this presentation will be local, not on the NET.

Thanks in advance for your help!

~dmason165

Jona
04-12-2003, 11:20 AM
Well, it'd probably be easier to do on the local disk, actually. I'll help you in a minute, sorry, I have to go get some yard work done! :(

Be back in a few (like 30 - 45 minutes!)...

dmason165
04-12-2003, 11:28 AM
Thanks Jona!

Jona
04-12-2003, 12:40 PM
Okay, here goes...

<html><head>
<SCRIPT LANGUAGE="JavaScript">
var firsttime = new Date();
var timeSpent;
function checkspeed() {
}
cookie_name = "x_time";

function setCookie() {
var secondtime = new Date();
difference = secondtime - firsttime;
timeSpent=difference;
alert(timeSpent)
if(difference <= 60000 ){
alert("You're reading awfully fast. Don't you think you should take it a bit more slowly?");
return false;
} else { return true; }

if(document.cookie != document.cookie)
{index = document.cookie.indexOf(cookie_name);}
else { index = -1;}
if(index == -1){
document.cookie=cookie_name+"="+timeSpent+"; expires=Monday, 04-Apr-2010 05:00:00 GMT";
} }

function getName() {
if(document.cookie){
index = document.cookie.indexOf(cookie_name);
if(index != -1){
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf(";", index);
if(nameend == -1){nameend = document.cookie.length;}
timeSpent = document.cookie.substring(namestart, nameend);
return timeSpent;
} } }
timeSpent=getName();
</SCRIPT></head><body>
<h1>Content</h1><br>
<p>Lorem ipsum solor dit amet....</p>
<input type=button value="Set" onClick="setCookie();"><br>
<input type=button value="Retreive" onClick="getName();"><br>
</body></html>

dmason165
04-13-2003, 12:22 AM
Working with the cookie Jona provided me, I have the following code:

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

function getCookie(TimeSpent) {
if (document.cookie.length > 0) {
begin = document.cookie.indexOf(TimeSpent+"=");
if (begin != -1) {
begin += TimeSpent.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end));
}
}
return null;
}

function setCookie(name, value, Expiration) {
var Expiration = new Date();
Expiration.setTime(Expiration.getTime() + 1000 * 60 * 60 * 24 * 7);
NewTime = new Date();
TimeRemaining = NewTime - StartTime;
if(TimeRemaining <= 60000 ){
alert("You're reading awfully fast. Don't you think you should take it a bit more slowly?");
return false;
}
else {
return true;
}
if(document.cookie != document.cookie) {
index = document.cookie.indexOf(TimeSpent);
}
else {
index = -1;
}
if(index == -1) {
document.cookie=TimeSpent+"="+TimeRemaining+"; expires=" + Expiration.toGMTString();
}
}
-->
</script>
</head>

<body>
This link will allow users to view <a href="SomeLink1.htm">refernced material</a>. <br>
This link goes to the next page and is timed. <a href="SomeLink2.htm" onClick="return setCookie();">Next
Page</a>
</body>
</html>

It ain't workin':(

Now that I've played around with cookies ALL DAY today, I don't think what I need is as far-fetched as I thought it would be.

Working with the following function:

<html>
<head>
<title>Untitled</title>
<script language="Javascript" type="text/javascript">
<!--
firsttime = new Date();

function checkspeed() {
secondtime= new Date();
difference = secondtime - firsttime;
if (difference <= 60000 ){
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>

<a href="next.htm" onClick="return checkspeed()">leave</a>

</body>
</html>

all I need is a cookie that stores the value of this function when the user follows an unrestricted link on the page and then retrieves the value to continue the timer when the user returns.

In short, the cookies will keep the timer from starting over.

What is not working in my cookies. Please help.

~Darron

Jona,

Thanks again!

Nevermore
04-13-2003, 08:41 AM
Sorry if this looks like I'm cutting you out, Jona, I got a PM about this and I haven't had time to check your code. I just made my own. This code definately works, although I haven't checked that the expirery date is correct. I don't want to wait a week to check, you see!


<html>
<head>
<title>Untitled</title>
<script language="Javascript" type="text/javascript">
<!--

firsttime = new Date();


//Cookie Part
function SetCookie() {
var today = new Date();
var expire = new Date();
var nDays=7
var cookieName="Time";
var cookieValue=firsttime;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}

function ReadCookie() {
var NameOfCookie="Time";
if(document.cookie.length > 0)
{
begin = document.cookie.indexOf(NameOfCookie+"=");
if(begin != -1)
{
// our cookie was set.
// The value stored in the cookie is returned from the function
begin += NameOfCookie.length + 1;
end = document.cookie.indexOf(";",begin);
if(end == -1) end = document.cookie.length;
firsttime=(document.cookie.substring(begin,end));
}
}
}

//Speed checker
function checkspeed() {
ReadCookie();
secondtime= new Date();
difference = secondtime - firsttime;
if (difference <= 60000 ){
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>

<a href="next.htm" onClick="return checkspeed()">leave</a>
<a href="previous.html" onclick="SetCookie();">Back</a>
</body>
</html>



Each of the links to leave need to run the function as with the back link. I did consider using an onUnload link, which would trigger even if they use the browsers back button, but it won't always work.

Jona
04-14-2003, 11:51 PM
Sorry, I couldn't provide a solution, Dmason. I didn't have access to the Internet for three days! (Darn, I had all of these emails!)