IH666
asked on
greeting repeat visitors with javascript
How can I write a greeting for visitors who have previously visited today? It has to reset at midnight each day.
First time they visit it sets a cookie that expires at midnight, next time it detects the cookie and triggers a greeting. I know the logic but don't know how to put in javascript.
First time they visit it sets a cookie that expires at midnight, next time it detects the cookie and triggers a greeting. I know the logic but don't know how to put in javascript.
ASKER
Hi
I'm not very good at JS. How can I strip the code of user prompt and just set it to trigger DoGreeting() function?
I'm not very good at JS. How can I strip the code of user prompt and just set it to trigger DoGreeting() function?
Sure, just change;
<body onLoad="checkCookie()">
to:
<body onLoad="DoGreeting()">
You of course would define DoGreeting(), something like this:
function DoGreeting()
{
username=getCookie('userna me')
if (username!=null)
alert('Welcome again, '+username+'!');
else
setCookie('username','joe' ,365); // <-- Change the MIDDLE PARAM 'joe' to whatever YOU want displayed!
}
You could then completely remove checkCookie() altogether. Now it doesn't prompt you to enter a name, it just automatically puts a name into the cookie. Right now it always puts joe, you can change that depending on the person, or if you want a general message like "Hello Again" I can do that for you.
Joe P
<body onLoad="checkCookie()">
to:
<body onLoad="DoGreeting()">
You of course would define DoGreeting(), something like this:
function DoGreeting()
{
username=getCookie('userna
if (username!=null)
alert('Welcome again, '+username+'!');
else
setCookie('username','joe'
}
You could then completely remove checkCookie() altogether. Now it doesn't prompt you to enter a name, it just automatically puts a name into the cookie. Right now it always puts joe, you can change that depending on the person, or if you want a general message like "Hello Again" I can do that for you.
Joe P
ASKER
Yes please. I need make it just a generic function that shows everyone the same thing. I'm trying to keep it as compact as possible.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Does this look right to you:
<script type="text/javascript"><--
if (getCookie('v')!=null)
{
// do greeting
}
else
{
var d1 = new Date();d1.setHours(23);d1. setMinutes (59);d1.se tSeconds(5 9);var d2 = new Date();var diff = d1-d2;
document.cookie="v=1;expir es="+diff
}
//--></script>
(for sake of minimalism)
<script type="text/javascript"><--
if (getCookie('v')!=null)
{
// do greeting
}
else
{
var d1 = new Date();d1.setHours(23);d1.
document.cookie="v=1;expir
}
//--></script>
(for sake of minimalism)
ASKER
There was a slight bug with dates. No need to subtract d1-d2, as expires accepts d1 value.
This is as short as I could make it:
<script type="text/javascript"><!- -
if (document.cookie.indexOf(" v=0")>-1)
{
document.write("welcome back!");
}
else
{
var d = new Date();d.setHours(23);d.se tMinutes(5 9);d.setSe conds(59);
document.cookie="v=0;expir es="+d;
document.write("first visit!");
}
//--></script>
Thanks for the help.
This is as short as I could make it:
<script type="text/javascript"><!-
if (document.cookie.indexOf("
{
document.write("welcome back!");
}
else
{
var d = new Date();d.setHours(23);d.se
document.cookie="v=0;expir
document.write("first visit!");
}
//--></script>
Thanks for the help.
Sure! =)
Here is a nice couple functions to work with cookies in javascript:
http://www.w3schools.com/js/js_cookies.asp
If you don't already have some cookie functions you should use the setCookie() getCookie() methods there. Great start, very flexable, now for setting the expire time.
I modified setCookie() a little, instead of expire days (the common procedure) I worked out some code to find the milliseconds until midnight, and then the cookie will expire. Here is code in a sample page: (taken from a w3schools example)
<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.in
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.inde
if (c_end==-1) c_end=document.cookie.leng
return unescape(document.cookie.s
}
}
return null
}
function setCookie(c_name,value,exp
{
var d1 = new Date();
d1.setHours(23);
d1.setMinutes(59);
d1.setSeconds(59);
var d2 = new Date();
var diff = d1-d2;
document.cookie=c_name+ "=" +escape(value)+
((expiretime==null) ? "" : "; expires=" + diff)
}
function checkCookie()
{
username=getCookie('userna
if (username!=null)
{alert('Welcome again '+username+'!')}
else
{
username=prompt('Please enter your name:',"")
if (username!=null||username!
{
setCookie('username',usern
}
}
}
</script>
</head>
<body onLoad="checkCookie()">
<script type="text/javascript">
</script>
</body>
</body>
</html>
The only real change I did to the code was:
var d1 = new Date();
d1.setHours(23);
d1.setMinutes(59);
d1.setSeconds(59);
var d2 = new Date();
var diff = d1-d2;
document.cookie=c_name+ "=" +escape(value)+
((expiretime==null) ? "" : "; expires=" + diff)
That creates a date which is the current time, it then sets it to a second before midnight, creates another date which is the current time, finds the difference and then sets expires to just that. =)
Joe P