« Previous entry | Next entry » Browse > Snippets
Skip to comments (4)
Cookie management class {hash-table}
Posted by silverstrike on Jan 27 2007 @ 14:27 :: 4128 unique visits
I've added as much comments as I possibly can, to make the code more readable.I would be happy to receive comments and suggestions about the code.
Update:
- It seems that my previous method of checking was flawed. If the cookie name, contained another cookie name at the start, than two matches would be found (of course, the script only return the first match).
- Considering that one page could call a cookie more that once (and sometimes, allot more) I've added a check to see if the array has changed since the previous calling. If so, recreate the array.
Update[Feb 8, 2007]:
- After some subjecting the class for review, I received a correction. When splitting the 'document.cookie' to an array, I used the semi-colon as the argument for the split method - and then checked three times, to check that the argument 'sName' is genuine. The solution was to split the semi-colon following by a space. That made the condition 'sGetNames[i].search(sName+"=") == 0' the only one needed.
- Also, I assigned a variable on every iteration of the for..loop, although it is only being used if the match is found. the assignment was redundant (it is only being used once) and was removed completely.
- Added: description for the class usage.
Update[Feb 10, 2007]:
- The 'Write' method was updated to run the code that determines the lifespan of the cookie only in the event that the 'iDays' argument was specified.
CODE: JAVASCRIPT
var Cookie = {
//Read the cookie -
// @param sName {type: string} The cookie name. [required]
Read: function(sName){
//Split all the cookies to a 'name=value' pair {return: array}.
if(typeof sGetNames == "undefined" || sGetNames != document.cookie) sGetNames = document.cookie.split("; ");
//Loop through every cookie from the recieved array.
for(var i=0; i<sGetNames.length; i++){
//If the the name matches, return the value of the cookie {return: string}.
if((sGetNames[i].search(sName+"=") == 0)){
return sGetNames[i].substring(sGetNames[i].search("=")+1, sGetNames[i].length);
}
}
//return false, if match was not made.
return false;
},
//Write the cookie -
// @param sName {type: string} The cookie name. [required]
// @param sValue {type: string} The cookie value. [required]
// @param iDays {type: integer} The cookie lifespan in days. [optional]
Write: function(sName, sValue, iDays){
if(iDays){
//Get the current date
var dCurrent = new Date();
//Set the date to the 'iDays' defined lifespan. {return: date object}
dCurrent.setTime(dCurrent.getTime()+(iDays*24*60*60*1000));
var sExp = "; expires="+dCurrent.toGMTString();
}else sExp = ""; //If 'iDays' was not defined, do not set an expiry date
//Concatenate the cookie.
document.cookie = sName.concat("=", sValue, sExp, "; path=/");
},
//Remove the cookie -
// @param sName {type: string} The cookie name. [required]
Remove: function(sName){
//Using the Cookie.Write method, set the cookie to expire yesterday.
this.Write(sName, "", -1);
}
};
//Read the cookie -
// @param sName {type: string} The cookie name. [required]
Read: function(sName){
//Split all the cookies to a 'name=value' pair {return: array}.
if(typeof sGetNames == "undefined" || sGetNames != document.cookie) sGetNames = document.cookie.split("; ");
//Loop through every cookie from the recieved array.
for(var i=0; i<sGetNames.length; i++){
//If the the name matches, return the value of the cookie {return: string}.
if((sGetNames[i].search(sName+"=") == 0)){
return sGetNames[i].substring(sGetNames[i].search("=")+1, sGetNames[i].length);
}
}
//return false, if match was not made.
return false;
},
//Write the cookie -
// @param sName {type: string} The cookie name. [required]
// @param sValue {type: string} The cookie value. [required]
// @param iDays {type: integer} The cookie lifespan in days. [optional]
Write: function(sName, sValue, iDays){
if(iDays){
//Get the current date
var dCurrent = new Date();
//Set the date to the 'iDays' defined lifespan. {return: date object}
dCurrent.setTime(dCurrent.getTime()+(iDays*24*60*60*1000));
var sExp = "; expires="+dCurrent.toGMTString();
}else sExp = ""; //If 'iDays' was not defined, do not set an expiry date
//Concatenate the cookie.
document.cookie = sName.concat("=", sValue, sExp, "; path=/");
},
//Remove the cookie -
// @param sName {type: string} The cookie name. [required]
Remove: function(sName){
//Using the Cookie.Write method, set the cookie to expire yesterday.
this.Write(sName, "", -1);
}
};
CODE: JAVASCRIPT
Cookie.Read("cookie_name");
Cookie.Write("cookie_name", "cookie_value", [4]); //The brackets ([ ]) signifies optional value.
Cookie.Remove("cookie_name");
Cookie.Write("cookie_name", "cookie_value", [4]); //The brackets ([ ]) signifies optional value.
Cookie.Remove("cookie_name");
4 comments posted so far
Add your own »
2. On Jun 25 2009 @ 04:09 guest wrote:
DVD Audio RipperFree DVD Audio Ripper
DVD Audio Ripper for Mac
DVD to MP4 Converter
DVD to MP4 Converter for Mac
DVD to iPod Converter
free DVD to iPod Converter
DVD to iPod Converter for Mac
DVD Ripper
Diablo 2 CD Key
Diablo 2 CD Keys
Free DVD Ripper
DVD Ripper for Mac
NFL Jerseys
Scoccer Jerseys
Hockey Jerseys
MLB Jerseys
NHL Jerseys
Nike Sneakers
3. On Jul 14 2009 @ 08:11 guest wrote:
AVI to DVD Converter,AVI to DVD Creator,iPhone Ringtone Maker for Mac,AVI Converter OS X,VOB Converter OS X,AVCHD Video Converter,FLV Converter,PowerPoint Converter,AVCHD Converter,Blue-Ray ripper,Rip Blue Ray,FLV to MOV Mac,VOB to DVD,HD Video Converter,iPod Playlist Transfer4. On Nov 20 2009 @ 13:23 guest wrote:
<p>Whatever your fashion calling is or isn't, nothing says dedication like <a href="www.nfldream.net">NFL football jerseys</a>.<br><a href="www.purpleuggsonline.net">Ugg boots</a> are fast becoming one of the fashion trends of the decade<br>
get yourself a brand new pair of <a href="www.knittedugg.com">cheap Uggs</a>.</p>
1. On Jun 25 2009 @ 04:09 guest wrote:
<a href=http://www.dvdtomp4converter.net>DVD to MP4 Converter</a><a href=http://www.dvdtomp4converter.net/mac>DVD to MP4 Converter for Mac</a>
<a href=http://www.freedvdtoipodconverter.net>DVD to iPod Converter</a>
<a href=http://www.freedvdtoipodconverter.net>free DVD to iPod Converter</a>
<a href=http://www.freedvdtoipodconverter.net/mac>DVD to iPod Converter for Mac</a>
<a href=http://www.free-dvdripper.com>DVD Ripper</a>
<a href=http://www.cdkey-store.com>Diablo 2 CD Key</a>
<a href=http://www.cdkey-store.com>Diablo 2 CD Keys</a>
<a href=http://www.free-dvdripper.com>Free DVD Ripper</a>
<a href=http://www.free-dvdripper.com/dvd-ripper-for-mac.html>DVD Ripper for Mac</a>
<a href=http://www.freedvdaudioripper.com>DVD Audio Ripper</a>
<a href=http://www.freedvdaudioripper.com>Free DVD Audio Ripper</a>
<a href=http://www.freedvdaudioripper.com/mac>DVD Audio Ripper for Mac</a>
<a href=http://www.jerseysvip.com>NFL Jerseys</a>
<a href=http://www.jerseysvip.com>Scoccer Jerseys</a>
<a href=http://www.jerseysvip.com>Hockey Jerseys</a>
<a href=http://www.jerseysvip.com/mlb-jerseys-c-42.html>MLB Jerseys</a>
<a href=http://www.jerseysvip.com/nhl-jerseys-c-32.html>NHL Jerseys</a>
<a href=http://www.firstsneakers.com>Nike Sneakers</a>