Link to home
Start Free TrialLog in
Avatar of IH666
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.
Avatar of BogoJoker
BogoJoker

Hi IH666,

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.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return null
}

function setCookie(c_name,value,expiretime)
{
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('username')
if (username!=null)
  {alert('Welcome again '+username+'!')}
else
  {
  username=prompt('Please enter your name:',"")
  if (username!=null||username!="")
    {
    setCookie('username',username,365)
    }
  }
}
</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
Avatar of IH666

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?
Sure, just change;
<body onLoad="checkCookie()">
to:
<body onLoad="DoGreeting()">

You of course would define DoGreeting(), something like this:

function DoGreeting()
{
username=getCookie('username')
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
Avatar of IH666

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
Avatar of BogoJoker
BogoJoker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IH666

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.setSeconds(59);var d2 = new Date();var diff = d1-d2;
  document.cookie="v=1;expires="+diff
}
//--></script>

(for sake of minimalism)
Avatar of IH666

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.setMinutes(59);d.setSeconds(59);
  document.cookie="v=0;expires="+d;
  document.write("first visit!");
}
//--></script>

Thanks for the help.
Sure! =)