The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 06:19:00 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Efficient local (client) storage of cookies  (Read 2777 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: April 23, 2007, 09:31:47 AM »

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 machine. 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 machine

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!

Code:
// ----------------------------------------------------------- //
//                       localStorage                          //
// ----------------------------------------------------------- //
function localStorage() { this.clear(); }
localStorage.prototype.clear = function()
{
this.fileName = new String();
}
localStorage.prototype._getRaw = function()
{
var rawBuff = document.cookie;
var cookieRegExp = new RegExp("\\b" + this.fileName + "=([^;]*)");
theValue = cookieRegExp.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[i].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 stripAttributeRegExp = new RegExp("(^|/&)" + theName + "=[^&]*&?");
rawBuff = rawBuff.replace(stripAttributeRegExp, "$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 extractMultiValueCookieRegExp = new RegExp("\\b" + theName + "=([^;&]*)");
resValue = extractMultiValueCookieRegExp.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 stripAttributeRegExp = new RegExp("(^|&)" + theName + "=[^&]*&?");
rawBuff = rawBuff.replace(stripAttributeRegExp, "$1");
if (rawBuff.length != 0) { rawBuff += "&"; }
} else rawBuff = "";

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

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!