Jump to content

Cookie Object


Chocolate570

Recommended Posts

Hey guys,I've been working on a cookie object for the upcoming Zoodles toolkit.Unfortunately, I don't currently have the resources to test this properly.That's why I need you to do the following:1) Test this in as many browsers as you have2) Tell me if it works. If not, why not.3) Point out unnecessary code that I can remove. (it's currently around 70-80 lines of code, and it shouldn't be that long.)I've taken the liberty to comment as much as I can, so you can understand what the heck was going on in my mind when I wrote htis. :)Usage:This to set a cookie:Cookie.set(cookie name, cookie value);And to get a cookie;Cookie.get(cookie name, cookie expiration);This is how to use the expiration system:You have 5 keywords. Years, months, days, hours, and minutes.You have to use a number and then one of the keywords. (currently, it disregards the number. I need to figure out a way to implement that. :) For now, you can just do within the year, or within the month, or within the day, or within the hour, or within the minute.)For example.Cookie.get(username,"1 months");NOTE THE S on MONTHS.If the username cookie is older than 1 month, it'll return null, otherwise it'll return what the cookie contains.Thanks so much guys. Here's the code:

<script type="text/javascript">//////////////////////////////////E-Z JavaScript Cookie Object////By Chocolate570 for Zoodles ////Made in 2006 || Version One ////////////////////////////////////Start cookie object.Cookie={  set:function(name,value) {    //The set function.    //Takes 2 variables, name and value.    if(name && value) {      date=Date().split("(")[0];      document.cookie=escape(name+"="+value+"&"+date);    } else {      return "Sorry, you have incorrectly used the cookie object.";    }  }  get:function(name,exp) {    //Name is the name of the variable.    //Exp is the expiration date in this format:    //X y    //Where X is a number and y is a date format. Available:    //Years, Months, Days, Hours, Minutes    y=Date();    cooSpl=document.cookie.split(";");    //*Loop through all of the cookie values.*//    for(i=0;i<cooSpl.length;i++) {      if(cooSpl[i].split("=")[0]==name) {        //*Why hello, we've found the correct cookie.*//        val=cooSpl[i].split("=")[1].split("&")[0];        date=cooSpl[i].split("=")[1].split("&")[1];        //*Just splitting apart the value and the date, held in their respective variables.*//        x=new Date(date);        years=(x.getFullYear()==y.getFullYear());        months=(x.getMonth()==y.getMonth());        days=(x.getDate()==y.getDate());        hours=(x.getHours()==y.getHours());        minutes=(x.getMinutes()==y.getMinutes());        //* The above are just used in the if statements to check on the date argument.*//        //* Start Date Switch Statement *//        switch(exp.split(" ")[1].toLowerCase()) {          case "years":	    if(years) {              return val;            }            return null;          break;          case "months":            if(years && months) {              return val;            }            return null;          break;          case "days":            if(years && months && days) {              return val;            }            return null;          break;          case "hours":            if(years && months && days && hours) {              return val;            }            return null;          break;          case "minutes":            if(years && months && days && hours && minutes) {              return val;            }            return null;          break;          default:            return null;          break;        }      }    }  }}</script>

Thanks so much. :)Choco

Link to comment
Share on other sites

First off I reformatted your code and fixed a couple typos I saw (you forgot the , between the object functions) so it was easier to read

//////////////////////////////////E-Z JavaScript Cookie Object////By Chocolate570 for Zoodles ////Made in 2006 || Version One ////////////////////////////////////Start cookie object.var Cookie ={    set: function(name,value)     {        //The set function.        //Takes 2 variables, name and value.        if(name && value)         {            date=Date().split("(")[0];            document.cookie=escape(name+"="+value+"&"+date);        }         else         {            return "Sorry, you have incorrectly used the cookie object.";        }    },        get: function(name,exp)     {        //Name is the name of the variable.        //Exp is the expiration date in this format:        //X y        //Where X is a number and y is a date format. Available:        //Years, Months, Days, Hours, Minutes        y=Date();        cooSpl=document.cookie.split(";");                //*Loop through all of the cookie values.*//        for(i=0;i<cooSpl.length;i++)         {            if(cooSpl[i].split("=")[0]==name)             {                //*Why hello, we've found the correct cookie.*//                val=cooSpl[i].split("=")[1].split("&")[0];                date=cooSpl[i].split("=")[1].split("&")[1];                //*Just splitting apart the value and the date, held in their respective variables.*//                x=new Date(date);                years=(x.getFullYear()==y.getFullYear());                months=(x.getMonth()==y.getMonth());                days=(x.getDate()==y.getDate());                hours=(x.getHours()==y.getHours());                minutes=(x.getMinutes()==y.getMinutes());                //* The above are just used in the if statements to check on the date argument.*//                //* Start Date Switch Statement *//                switch(exp.split(" ")[1].toLowerCase())                 {                    case "years":                        if(years)                         {                            return val;                        }                        return null;                        break;                    case "months":                        if(years && months)                         {                            return val;                        }                        return null;                        break;                    case "days":                        if(years && months && days)                         {                            return val;                        }                        return null;                        break;                    case "hours":                        if(years && months && days && hours)                         {                            return val;                        }                        return null;                        break;                    case "minutes":                        if(years && months && days && hours && minutes)                         {                            return val;                        }                        return null;                        break;                    default:                        return null;                        break;                }            }        }    }}

I am using the object like this

<html><head>    <script src="cookie.js"></script>    <script>            Cookie.set("mycookie","somevalue");        document.write(Cookie.get("mycookie","1 months"));        </script></head><body>    </body></html>

It keeps writing "undefined", I even tryed puting the document.write in a setTimeout for 10 seconds incase it wasn't finished writing the cookie but got the same result.

Link to comment
Share on other sites

Using aspnetguy's method of setting and getting the cookie created a cookie on my computer with the following settings:

Name: Content: mycookie%3Dsomevalue%26Fri%20Dec%2015%202006%2008%3A29%3A24%20GMT-0800%20Host: Path: /C:/Documents%20and%20Settings/[My Username]/Desktop/Send For: Any type of connectionExpires: at end of session

Maybe it's because the Name attribute is blank that we get the "undefined". Also, since the cookie expires at the end of the session, if I were to close my browser the cookie would automatically be deleted. We wouldn't be able to come back at another date to use the Cookie.get method.It might be more relevant to have data like the following:

Name: [SomeGlobalCookieName]Content: mycookie=somevalue|mysecondcookie=someothervalue|userid=tlvCIEH64L0dwfxziTxHost: Path: /C:/Documents%20and%20Settings/[My Username]/Desktop/Send For: Any type of connectionExpires: Sunday, January 17, 2038 4:00:00 PM

Link to comment
Share on other sites

Using aspnetguy's method of setting and getting the cookie created a cookie on my computer with the following settings:
Name: Content: mycookie%3Dsomevalue%26Fri%20Dec%2015%202006%2008%3A29%3A24%20GMT-0800%20Host: Path: /C:/Documents%20and%20Settings/[My Username]/Desktop/Send For: Any type of connectionExpires: at end of session

Maybe it's because the Name attribute is blank that we get the "undefined". Also, since the cookie expires at the end of the session, if I were to close my browser the cookie would automatically be deleted. We wouldn't be able to come back at another date to use the Cookie.get method.It might be more relevant to have data like the following:

Name: [SomeGlobalCookieName]Content: mycookie=somevalue|mysecondcookie=someothervalue|userid=tlvCIEH64L0dwfxziTxHost: Path: /C:/Documents%20and%20Settings/[My Username]/Desktop/Send For: Any type of connectionExpires: Sunday, January 17, 2038 4:00:00 PM

I forgot to unescape the cookie data. :)It still doesn't work though. :'(Any suggestions?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...