Hidden > Javascript
How to store your cookies

Hallo, this is the only non fruitarian/vegan orientated part of this website.. I've had so much trouble getting the navigation system to work correctly, and eventually decided to sit down and write my own Cookie utility.. If you are into WebPage design, and wish to utilize cookies, then please feel free to use this code: (Check back here for possible future updates to this function). - the code for the button clock an be found below.


//CookieJar Version 1.0 - January 22nd 2001 - Mango Wodzak - www.fruitnut.net
//CookieJar Version 1.1 - January 10th 2002 - Added "show" and "rename".
//Action can be "get","set","anex","count" or "delete".

var Action,CookieName,Ingredients,UseBy=new Date();
UseBy.setTime(UseBy.getTime()+(10*365*24*60*60*1000)); // 10 years from now.
function CookieJar(Action,CookieName,Ingredients) {
  if (Action!="set") {
    if (Action=="anex"||Action=="rename") var init=Ingredients;
    StartPos=document.cookie.indexOf(CookieName+"=");
    if (StartPos==-1)Ingredients="";
    if (StartPos!=-1) {
      WorkIt=document.cookie.substring(StartPos,document.cookie.length);
      StartPos=WorkIt.indexOf("=")+1;
      EndPos=WorkIt.indexOf(";");
      if (EndPos==-1) EndPos=WorkIt.length;
      Ingredients=WorkIt.substring(StartPos,EndPos);
    }
  }
  if (Action!="get") {
    if (Action=="show") {alert(CookieName+"="+Ingredients); return(Ingredients);}
    if (Action=="rename") CookieName=init;
    if (Action=="anex") Ingredients=Ingredients+"-"+init;
    if (Action=="count"){Ingredients=escape(Ingredients);Ingredients++;}
    if (Action=="delete")document.cookie=CookieName+"=;path=/;expires=Thu, 01-Jan-70 00:00:01 GMT";
    else document.cookie=CookieName+"="+Ingredients+";expires="+UseBy.toGMTString()+";path=/";
  }
  ;return(Ingredients);
}


The way I use this, is I put the code as it stands in a file named CookieJar.js and then I stick the following line in the document <HEAD> section of each of my HTML files that call the function:

<SCRIPT LANGUAGE=JAVASCRIPT SRC=CookieJar.js></SCRIPT>


I can then call it from a javascript within my html body as follows:
  
CookieJar("set","CookieName","CookieContent"); - "CookieName" = "CookieContent"
VariableName=CookieJar("get","CookieName"); - "VariableName" = "CookieName"
CookieJar("anex","CookieName","CookieContent"); - the original content of "CookieName" remains untouched, "CookieContent" gets added to the end of it.
CookieJar("count","CookieName"); - if CookieName is a number, increase it by 1.
CookieJar("delete","CookieName"); - deletes the contents of "CookieName"

The Code for the button clock is as follows:

<SCRIPT LANGUAGE=JAVASCRIPT>
//Button Clock Version 1.0 - January 22nd 2000 - Mango Wodzak - www.fruitnut.net

function Wat(s) {
  if ("INPUT"==event.srcElement.tagName) event.srcElement.className=s;
}
if (navigator.appName != "Netscape") {
  function ButtonClock() {
    day = new Date();
    hour = day.getHours();
    mins = day.getMinutes();
    secs = day.getSeconds();
    if (hour <= 9) hour="0"+hour;
    if (mins <= 9) mins="0"+mins;
    if (secs <= 9) secs="0"+secs;
    time=hour+":"+mins+":"+secs;
    document.form.button.value=time;
    setTimeout('ButtonClock()',1000);
  }
  document.write('<form name="form" onMouseover="Wat(\'start\')" onMouseout="Wat(\'end\')">');
  document.write('<input TYPE=button class=Butt value="nul" name=button>');
  document.write('</form>');
  onError = null;
  ButtonClock();
}

</SCRIPT>