Click to See Complete Forum and Search --> : cookies help
2 peachy
04-04-2003, 01:35 PM
I have script I need to change to do the following... but not sure how...
I need to have it....
Display a popup message to welcome the user by name.
Tell the user how long it has been since their last visit.
Display the number of times the user has visited your page.
Reset the expiration to be 30 days from the time of the last visit.
the script ......
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<SCRIPT LANGUAGE="JavaScript">
<!--
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "");
document.cookie = curCookie;
}
// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}
// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}
// create an instance of the Date object
var now = new Date();
// fix the bug in Navigator 2.0, Macintosh
fixDate(now);
// cookie expires in one year (actually, 365 days)
// 365 days in a year
// 24 hours in a day
// 60 minutes in an hour
// 60 seconds in a minute
// 1000 milliseconds in a second
now.setTime(now.getTime() + 30 * 24 * 60 * 60 * 1000);
var username = getCookie("username");
// if the cookie wasn't found, ask for the name
if (!username) {
username = prompt("Please enter your name:", "");
}
// set the new cookie
setCookie("username", username, now);
if (username) {
alert("Welcome back, " + username + ".");
setCookie("username", username, now);
} else
alert("You refused to enter your name.");
// -->
</SCRIPT>
<body>
</body>
</html>
2 peachy
04-05-2003, 06:38 PM
is there anyone that can help me with this ???
please please please
peachy
2 peachy
04-06-2003, 04:20 PM
I need to have that script... do this...
Display a popup message to welcome the user by name.
Tell the user how long it has been since their last visit.
Display the number of times the user has visited your page.
Reset the expiration to be 30 days from the time of the last visit.
I am not sure how to change it ...so it does the above...
I have a popup that welcomes the user back by name....
I need to learn how to make it include how long it has been since the user was last on the page....and how many times they have visited the page...
And set the cookie or cookies for 30 days, which resets to 30 days from the time of the last visit.
2 peachy
04-06-2003, 07:02 PM
I am not asking you to write the code for me Dave,
If I knew exactly what I need to do, I would do it....
ok....if I am understanding what you want me to do,
1 .... I need to have a popup, where the user is asked to type name... which I have.
var username = getCookie("username");
// if the cookie wasn't found, ask for the name
if (!username) {
username = prompt("Please enter your name:", "");
}
2. I have it set to expire in thirty days, I want tp learn how to make it reset the time to thirty days after each time the user goes back to the page.
// create an instance of the Date object
var now = new Date();
// fix the bug in Navigator 2.0, Macintosh
fixDate(now);
// cookie expires in thirty days(30 days)
// 24 hours in a day
// 60 minutes in an hour
// 60 seconds in a minute
// 1000 milliseconds in a second
now.setTime(now.getTime() + 30 * 24 * 60 * 60 * 1000);
I am not sure if this is even right... but I am trying to learn....
3. I need popup when the user returns, which welcomes them back by name.... I have that part.... but want to learn how to
add how long it has been since their last visit to the page and
how many times they have visited the page.
// set the new cookie
setCookie("username", username, now);
if (username) {
alert("Welcome back, " + username + ".");
setCookie("username", username, now);
} else
alert("You refused to enter your name.");
Thankyou for your help... and please understand.... I was not merely asking anyone to write the code for me... perhaps that is what it seemed like... for that I am sorry.
2 peachy
04-06-2003, 07:15 PM
yes... when I open the page.. the popup comes up and ask for user name....
That is all it needs to do
the next part... I have it set for the cookie to expire in thirty days although I am not sure if I did it right....
I also need to somehow make it so the expiration resets to thirty days each time the user visits the page, which I do not know how to do...
2 peachy
04-06-2003, 07:17 PM
this is entire code as I have it now...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<SCRIPT LANGUAGE="JavaScript">
<!-- begin
function setCookie(name, value, expires) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "");
document.cookie = curCookie;
}
// name - name of the cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}
// date - any instance of the Date object
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}
//end -->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!--begin
// create an instance of the Date object
var now = new Date();
// fix the bug in Navigator 2.0, Macintosh
fixDate(now);
// cookie expires in thirty days(30 days)
// 24 hours in a day
// 60 minutes in an hour
// 60 seconds in a minute
// 1000 milliseconds in a second
now.setTime(now.getTime() + 30 * 24 * 60 * 60 * 1000);
var username = getCookie("username");
// if the cookie wasn't found, ask for the name
if (!username) {
username = prompt("Please enter your name:", "");
}
// set the new cookie
setCookie("username", username, now);
if (username) {
alert("Welcome back, " + username + ".");
setCookie("username", username, now);
} else
alert("You refused to enter your name.");
// end -->
</SCRIPT>
<body>
</body>
</html>
2 peachy
04-06-2003, 08:05 PM
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}
A friend of mine said I needed that... but I dont understand it...
should I get rid of it ?
2 peachy
04-06-2003, 08:11 PM
I took it out.... and it still works.
ok if what I have for expiration time resets itself.
the last two things I need is to somehow make it so
my welcome back popup... also includes how long it has been since the last time the user visited the page... and how many times they visited the page...
Something like this...
Welcome Back Peachy,
It has been _____ since your last visit.
You have visited this page _____ times.
My popup works so far....
with the welcome back part....
I guess I need to make a function to calculate the time and the number of times ... ???? I am not sure....
2 peachy
04-06-2003, 08:33 PM
doe that mean that I need it in there ?
2 peachy
04-06-2003, 08:54 PM
ok.. will wait to see what he says...
how do I write the code to make it calculate the time between visits and add that information to my popup...
and the same thing with ... how many total visits to the page ?
2 peachy
04-06-2003, 11:42 PM
Dave, thankyou very much for your help.
I am sorry I am such a complete idiot when it comes to learning this...
have JavaScript The Difinitive guide in front of me...and have read through chapter 18 twice now... and still am not sure how to achieve my goals with this script.
Peachy
2 peachy
04-08-2003, 02:30 PM
ok.... i have redone this thing....
everything works.... but I dont know if I have it so it resets to 30 days every time the visitor returns to the page.
Thankyou so much for your help Dave, I am so grateful for your help....
if you could look at this and see if it is right, I would appreciate it...
peachy
2 peachy
04-08-2003, 02:46 PM
I am seeing a problem with my script....
when the popup comes up... it doesnt get the month, date and year....
it says Apr 8 1 then the time... got any ideas how I can fix this ?
peachy
2 peachy
04-08-2003, 05:52 PM
http://www.sonic.net/~iharper/Cookies/cookie.html
I uploaded it... the only thing there is the cookie script, as I want to get this working before I add anything else...
another question.
the first time the user goes there.... the popup says 0 visits which is correct I assume... but it says last visit was sometime in December... not sure why it is doing this...
seems like I goofed up this script... even though it is working..
peachy
2 peachy
04-09-2003, 10:13 AM
Thankyou very much...smiles
there is one problem, when the popup comes up.
it gives the month, day, year and time...
but it says Dec. 31,1969
peachy
Jona
04-09-2003, 06:18 PM
Originally posted by 2 peachy
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}
A friend of mine said I needed that... but I dont understand it...
should I get rid of it ?
Just thought I'd say something about this. I believe that it's a bug-fix for Macintosh computers.
Jona
04-09-2003, 06:30 PM
:p Sorry, I didn't see that... :o
2 peachy
04-09-2003, 07:56 PM
when I go back to the page, it updates how many times I have been there, and updates the time, but still says
December 31, 1969
peachy
2 peachy
04-09-2003, 10:58 PM
oops... sorry.... it is updated now
http://www.sonic.net/~iharper/Cookies/cookie.html
2 peachy
04-09-2003, 11:24 PM
I just updated it again...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Cookie Script</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function Who(info){
var VisitorName = GetCookie('VisitorName')
if (VisitorName == null) {
VisitorName = prompt("What is your name?");
SetCookie ('VisitorName', VisitorName, exp);
}
return VisitorName;
}
function When(info){
var lastVisit = GetCookie('WWhenH')
if (lastVisit = null) {
lastVisit = new Date();
} else {
lastVisit = new Date(lastVisit);
}
var dayOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][lastVisit.getDay()];
var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][lastVisit.getMonth()];
var day = lastVisit.getDate();
var year = lastVisit.getFullYear();
var hour = lastVisit.getHours();
var minute = lastVisit.getMinutes();
var second = lastVisit.getSeconds();
SetCookie ("WWhenH", (new Date()).getTime(), exp)
return (dayOfWeek+", "+month+" "+day+", "+year+" at "+hour+":"+minute+":"+second);
}
function Count(info){
var WWHCount = GetCookie('WWHCount')
if (WWHCount == null) {
WWHCount = 0;
}
else{
WWHCount++;
}
SetCookie ('WWHCount', WWHCount, exp);
return WWHCount;
}
function set(){
VisitorName = prompt("Who are you?");
SetCookie ('VisitorName', VisitorName, exp);
SetCookie ('WWHCount', 0, exp);
SetCookie ('WWhenH', 0, exp);
}
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
document.write
alert("Hello " + Who() + ".");
alert(" You've been here " + Count() + " time(s).");
alert(" Your last visit was " + When() +".");
// End -->
</SCRIPT>
</head>
<body>
</body>
</html>
If I didnt get it right, what am I missing.... I thought I copied exactly what you posted....
I am sorry I am such a pain.... and I do really appreciate your help and patience with me.
peachy
2 peachy
04-09-2003, 11:52 PM
grrrrrr.... I have uploaded it with the changes... I viewed the source..and it looks ok to me... but it still isnt working right for me... and now it is not updating the time either.
peachy
2 peachy
04-10-2003, 01:46 PM
Thankyou so very much for your help and patience Dave,
Proof there really are angels when someone like you takes the time and patience to help people they dont even know...
hugs to you.
peachy
2 peachy
04-10-2003, 02:17 PM
Hello Dave,
one last question, then I will get out of your hair, smiles.
right now, I have three alerts one with name, one with the number of visits, and one with the last date and time of visit.
alert("Hello " + Who() + ".");
alert(" You've been here " + Count() + " time(s).");
alert(" Your last visit was " + When() +".");
I have tried and tried to get this all on one popup on separate lines by adding \n where I want it to move to the next line...
Every time I have done this before, it worked, but now when I do it, the script stops working...
can you help me ?
peachy
2 peachy
04-10-2003, 05:55 PM
Thankyou so very much
peachy
sciguyryan
08-22-2003, 09:21 AM
hi,
do you mind if i use this code for my site?
I will of corse use uourname on the script.
if you will allow ti please E-mail me @ R.jones194@fsmail.net
thank you in advance.
2 peachy
08-25-2003, 12:02 PM
sure, go ahead
pari_mact
10-02-2003, 10:05 AM
The problem follows,
We are having some real time measured data. In this data we are having some out of place values because of the measurement error.
Following data is good sample, which doesn't have any out of place values.
0.397431
0.573151
0.8v556
1.89717
4.38306
8.44119
13.3435
19.881
27.8278
36.8083
46.027
57.2585
70.5046
86.584
104.927
124.78
146.263
167.314
But in the following data we are having some out of place values.
0.397431
-1.39480 - Out of place value
0.876556
1.89717
4.38306
8.44119
78.38692 - Out of place value
19.881
27.8278
36.8083
46.027
57.2X5
70.5046
86.584
104.927
124.78
146.263
167.314
What you need to do is, prepare an algorithm to solve the following
1) Find the out of place value.
2) Remove the out of place value and replace it with mathematically calculated value based on the remaining data.
and reply me at pari_mact@rediffmail.com ,, if u can do it..
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.