
![]() |
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 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 machineThe 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 RegExp = newRegExp("\b" + this.fileName + "=([^;]*)");theValue = cookie RegExp.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 RegExp = newRegExp("(^|/&![]() rawBuff = rawBuff.replace(stripAttribute RegExp, "$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 RegExp = newRegExp("\b" + theName + "=([^;&]*)");resValue = extractMultiValueCookie RegExp.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 RegExp = newRegExp("(^|&![]() rawBuff = rawBuff.replace(stripAttribute RegExp, "$1");if (rawBuff.length != 0) { rawBuff += "&"; } } else rawBuff = ""; rawBuff += theName + "=" + escape(theValue); document.cookie = this.fileName + "=" + rawBuff; } |

Thread Categories

![]() |
![]() |
Best of The Cache Home |
![]() |
![]() |
Search The Cache |
- Ajax
- Apache & mod_rewrite
- BlackHat SEO & Web Stuff
- C/++/#, Pascal etc.
- Database Stuff
- General & Non-Technical Discussion
- General programming, learning to code
- Javascript Discussions & Code
- Linux Related
- Mac, iPhone & OS-X Stuff
- Miscellaneous
- MS Windows Related
- PERL & Python Related
- PHP: Questions & Discussion
- PHP: Techniques, Classes & Examples
- Regular Expressions
- Uncategorized Threads