perkiset

Storage of cookies is really quite easy, but what with the limit of cookies-per-domain and size of each cookie, it is better if you can compact multiple values into a single location.

Here is the storage class that I use to keep lotso variables on the local

mac

 hine. Usage is simple:

<script>
storage = new localStorage();
storage.fileName = 'stateInfo';
storage.storeItem('aVariable', 'aValue');
aValue = storage.retrieveItem('aVariable');
storage.dropItem('aVariable'); // Eliminate a single value
storage.dropFile(); // clear the whole shebang from the local

mac

 hine

The fileName property is for you applications logical seperation of cookie groups. Functionally, this is the name that I use for the cookie on the local system. For you, it should be a meaningful name that represents the grouping of <these values> .

Enjoy!


// ----------------------------------------------------------- //
//                      localStorage                          //
// ----------------------------------------------------------- //
function localStorage() { this.clear(); }
localStorage.prototype.clear = function()
{
this.fileName = new String();
}
localStorage.prototype._getRaw = function()
{
var rawBuff = document.cookie;
var cookie

RegEx

 p = new

RegEx

 p("\b" + this.fileName + "=([^;]*)");
theValue = cookie

RegEx

 p.exec(rawBuff);
if (theValue != null) { theValue = theValue[1]; }
return theValue;
}
localStorage.prototype.asArray = function()
{
var outArr = new Object();
var rawBuff = this._getRaw(this.fileName);
if (rawBuff == undefined) { return false; }
var tempArr = rawBuff.match(/([^&]+)/g);
for (var i=0; i<tempArr.length; i++)
{
var parts = tempArr<>.match(/([^=]+)=(.*$)/);
var varName = parts[1];
var varValue = parts[2];
outArr[varName] = unescape(varValue);
}
return outArr;
}
localStorage.prototype.dropFile = function()
{
if (this.fileName)
{
var expiredDate = new Date();
expiredDate.SetMonth(-1);
var writeBuff = this.fileName + "=";
writeBuff += "expires=" + expiredDate.toGMTString();
document.cookie = writeBuff;
}
}
localStorage.prototype.dropItem = function(theName)
{
var rawBuff = readUnEscapedCookie(this.fileName);
if (rawBuff)
{
var stripAttribute

RegEx

 p = new

RegEx

 p("(^|/&Applause" + theName + "=[^&]*&?");
rawBuff = rawBuff.replace(stripAttribute

RegEx

 p, "$1");
if (rawBuff.length != 0)
{
var newBuff = this.fileName + "=" + rawBuff;
document.cookie = newBuff
} else { this.dropFile(); }
}
}
localStorage.prototype.enabled = function()
{
var cookiesEnabled = window.navigator.cookieEnabled;
if (!cookiesEnabled)
{
document.cookie = "cookiesEnabled=True";
cookiesEnabled = new Boolean(document.cookie).valueOf();
}
return cookiesEnabled;
}
localStorage.prototype.retrieveItem = function(theName)
{
var rawBuff = this._getRaw(this.fileName);
var extractMultiValueCookie

RegEx

 p = new

RegEx

 p("\b" + theName + "=([^;&]*)");
resValue = extractMultiValueCookie

RegEx

 p.exec(rawBuff);
if (resValue != null) { resValue = unescape(resValue[1]); }
return resValue;
}
localStorage.prototype.storeItem = function(theName, theValue)
{
var rawBuff = this._getRaw(this.fileName);
if (rawBuff)
{
var stripAttribute

RegEx

 p = new

RegEx

 p("(^|&Applause" + theName + "=[^&]*&?");
rawBuff = rawBuff.replace(stripAttribute

RegEx

 p, "$1");
if (rawBuff.length != 0) { rawBuff += "&"; }
} else rawBuff = "";

rawBuff += theName + "=" + escape(theValue);
document.cookie = this.fileName + "=" + rawBuff;
}


Perkiset's Place Home   Politics @ Perkiset's