INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Cookies

Let the user know when they last visited your web site by adrikev
Posted: 7 Aug 02 (Edited 9 Apr 12)

So, you want the user to know when they last visited your web site. This can be done using cookies (yum). This is how it works:

When a user makes a first visit to your web site he/she will see a message that says This is your first visit here. Then the script will write a cookie on the users machine that contains todays date and time. When the user returns to your site the script will read the cookie, return a message to the browser and overwrite the cookie with a new one that contains todays date and time. The new message will read:

Your last visit was on:
weekday month day at time PM/AM, year


Is this great or what? Here is how it is done.

CODE

<script language="JavaScript">
<!--
readCookie();

function readCookie() {
     if (document.cookie == "") {
    writeCookie();
        alertMessage();
     } else {
    var the_cookie = document.cookie;
    the_cookie = unescape(the_cookie);
    the_cookie_split = the_cookie.split(";");
    for (loop=0;loop<the_cookie_split.length;loop++) {
        var part_of_split = the_cookie_split[loop];
        var find_name = part_of_split.indexOf("nfti_date")
        if (find_name!=-1) {
            break;
        } // Close if
    } // Close for
    if (find_name==-1) {
        writeCookie();
    } else {
        var date_split = part_of_split.split("=");
        var last = date_split[1];
        last=fixTheDate(last);
        document.write('<font color="#000000" font size="1" font face="Verdana, Helvetica">Your last visit was on:<BR></font><font color="#FF0000" font size="1" font face="Verdana, Helvetica">'+last);
        document.write('</font>');
        writeCookie();
    } // Close if (find_name==-1)
      }
} // Close function readCookie()


function writeCookie() {
     var today = new Date();
     var the_date = new Date("December 31, 2023");
     var the_cookie_date = the_date.toGMTString();
     var the_cookie = "nfti_date="+escape(today);
     var the_cookie = the_cookie + ";expires=" + the_cookie_date;
     document.cookie=the_cookie
}

function alertMessage(){
     document.write('<font color="#000000" font size="1" font face="Verdana, Helvetica">This is your first visit here.</font>')
}

function fixTheDate(date) {
     var split = date.split(" ");
     var fix_the_time = split[3].split(":")
     var hours = fix_the_time[0]
     if (hours>=12) {
    var ampm="PM"
     } else {
    var ampm="AM"
     }
     if (hours > 12) {
    hours = hours-12
     }
     var new_time = hours+":"+fix_the_time[1]+" "+ampm
     var new_date = split[0]+" "+split[1]+". "+split[2]+" at "+new_time+", "+split[5]
     return new_date;
}
//-->
</script>

If you use a text editor to open the cookie that is set to the users machines, it will look something like this:

nfti_dateWed%20Aug%207%2012%3A35%3A45%20CDT%202002webdev/0252880076831079342369748411229507128*

Thats all there is to it. Play with the font styles, document.write statements and date/time formats to tweak the script to deliver the exact message you want.

WARNING changing the writeCookie() function might cause the script to generate errors. Don't change this one unless you know what you are doing.

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close