Created
December 14, 2013 18:38
-
-
Save mundry/7963081 to your computer and use it in GitHub Desktop.
Exact imitation of the localStorage object, but uses cookies to provide localStorage in browsers that don't support it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#Compatibility | |
// Note: The maximum size of data that can be saved is severely | |
// restricted by the use of cookies. With this algorithm, use the | |
// functions localStorage.setItem() and localStorage.removeItem() to | |
// add, change, or remove a key. The use of methods | |
// localStorage.yourKey = yourValue; and delete localStorage.yourKey; to | |
// set or delete a key *is not a secure way with this code*. You can | |
// also change its name and use it only to manage a document's cookies | |
// regardless of the localStorage object. | |
// | |
// Note: By changing the string | |
// "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/" to: "; path=/" | |
// (and changing the object's name), this will become a sessionStorage | |
// polyfill rather than a localStorage polyfill. However, this | |
// implementation will share stored values across browser tabs and | |
// windows (and will only be cleared when all browser windows have been | |
// closed), while a fully-compliant sessionStorage implementation | |
// restricts stored values to the current browsing context only. | |
if (!window.localStorage) { | |
Object.defineProperty(window, "localStorage", new (function () { | |
var aKeys = [], oStorage = {}; | |
Object.defineProperty(oStorage, "getItem", { | |
value: function (sKey) { return sKey ? this[sKey] : null; }, | |
writable: false, | |
configurable: false, | |
enumerable: false | |
}); | |
Object.defineProperty(oStorage, "key", { | |
value: function (nKeyId) { return aKeys[nKeyId]; }, | |
writable: false, | |
configurable: false, | |
enumerable: false | |
}); | |
Object.defineProperty(oStorage, "setItem", { | |
value: function (sKey, sValue) { | |
if (!sKey) { return; } | |
document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/"; | |
}, | |
writable: false, | |
configurable: false, | |
enumerable: false | |
}); | |
Object.defineProperty(oStorage, "length", { | |
get: function () { return aKeys.length; }, | |
configurable: false, | |
enumerable: false | |
}); | |
Object.defineProperty(oStorage, "removeItem", { | |
value: function (sKey) { | |
if (!sKey) { return; } | |
document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"; | |
}, | |
writable: false, | |
configurable: false, | |
enumerable: false | |
}); | |
this.get = function () { | |
var iThisIndx; | |
for (var sKey in oStorage) { | |
iThisIndx = aKeys.indexOf(sKey); | |
if (iThisIndx === -1) { oStorage.setItem(sKey, oStorage[sKey]); } | |
else { aKeys.splice(iThisIndx, 1); } | |
delete oStorage[sKey]; | |
} | |
for (aKeys; aKeys.length > 0; aKeys.splice(0, 1)) { oStorage.removeItem(aKeys[0]); } | |
for (var aCouple, iKey, nIdx = 0, aCouples = document.cookie.split(/\s*;\s*/); nIdx < aCouples.length; nIdx++) { | |
aCouple = aCouples[nIdx].split(/\s*=\s*/); | |
if (aCouple.length > 1) { | |
oStorage[iKey = unescape(aCouple[0])] = unescape(aCouple[1]); | |
aKeys.push(iKey); | |
} | |
} | |
return oStorage; | |
}; | |
this.configurable = false; | |
this.enumerable = true; | |
})()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment