Jim Hanson's

JavaScript Cookie Setting Example

This is the JavaScript source for the Welcome message you saw on the previous page.

First you need to define the JavaScript variables and functions inside <HEAD> tag of your HTML file. To do this, just copy the functions between <HEAD> and </HEAD> tags below. After the functions are defined in the <HEAD> portion of the page, you can insert the welcome( ) function call at the place in your document where you want to have that welcome message displayed.

Oh, yes! You can edit the welcome( ) function if you want to have other greetings displayed.

<HTML>
<TITLE>Your document title here</TITLE>
<HEAD>

<!-- Cookie Script Written By Maxim V. Kollegov --> 
<!-- Virtual_Max@geocities.com --> 
<!-- http://www.geocities.com/siliconvalley/lakes/8620/index.html --> 
<SCRIPT LANGUAGE="JavaScript">
<!-- to hide script contents from old browsers
var visitor
var lastvisited 
var countvalue
// this function extracts cookie by name and return value.
function getCookie(byname)
 {byname=byname+"=";
  nlen = byname.length;
  fromN = document.cookie.indexOf(byname)+0;
    if((fromN) != -1) 
     {fromN +=nlen 
      toN=document.cookie.indexOf(";",fromN)+0;
        if(toN == -1) 
          {toN=document.cookie.length;} 
        return unescape(document.cookie.substring(fromN,toN));
     } return null;
  }
//this function writes the new data to the viewer's cookie
function setCookie(name,value,time)
 {exp = new Date();
   if((name==null)||(value==null)) return false; 
   if(time==null) time=365*86400000; 
   exp.setTime(exp.getTime()+time);
   document.cookie =escape(name)+"="+escape(value)+"; "+"expires="+exp.toGMTString();
   return true;
 } 

//this function looks into cookie for counter, visitor and lastvisited
function checkAccess()
  {today=new Date();
   countvalue=getCookie("counter");
   visitor=getCookie("visitor");
   lastvisited=getCookie("lastvisited");
   if((countvalue==null)||(countvalue==""))
      {countvalue="0";
       visitor=prompt("Please, Enter Your Name:","");
       if((visitor==null) || (visitor==""))
          visitor="Incognito";
      }
   countvalue=parseInt(countvalue)+1;
   setCookie("counter",countvalue);
   setCookie("visitor",visitor);
   setCookie("lastvisited",today.toGMTString());
  }

//this function writes welcome message into browser.
function welcome()
  {checkAccess();
   s="<center><h2>Welcome, "+visitor+"! </h2>";        
   count = parseInt(countvalue);
   if(count==1)
     {s+="It's your first visit to my virtual home.</center>"}
   else
     {s+="You visited my page "+count+" times. ";
      s+="Last time it was "+lastvisited +"</center>";
     }
   document.writeln(s);
  }

// end hiding contents from old browsers -->
</SCRIPT>
</HEAD>

<BODY>

Insert the following JavaScript in your document and you will have the welcome message displayed in the viewer's browser.

.....Your document begins here.....

<SCRIPT LANGUAGE="JavaScript"> welcome(); </SCRIPT>

.....Rest of your document...........

</BODY>
</HTML>

Copyright © 1998 Jim Hanson