Last active
November 3, 2017 11:08
-
-
Save gustinmi/4945781 to your computer and use it in GitHub Desktop.
Simple JavaScript storage (webstorage or cookie based)
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
/** | |
* | |
* Component for utilizing local storage. It provides backup plan, if no WebStorage is available on client | |
* | |
*We support html5 storage, and cookie store if webStorage is not available. | |
* | |
*Cookies | |
* Are for older browsers only. A "session" cookie (one without expiration data) is available in all windows | |
* that have access to that domain until the browser is shut down. This is bad. | |
* 4096 chars limit | |
* 50 cookies per domain limit | |
* gets posted to server and back by every response | |
* | |
*WebStorage (session storage) | |
* A real session storage, referring to particular tab or window. Once tab is closed, that data disapperas | |
* 5MB limit | |
* | |
*WebStorage (local storage) | |
* Spans all windows on that domain | |
* * | |
*Usage in code | |
*To save: | |
* | |
* window.AppHtml5.save("key1","val1"); | |
* | |
*To read: | |
* | |
* var jsonObj = window.AppHtml5.load("key1"); | |
* IMPORTANT: | |
* Windows 8 / IE 10 needs workaround for exception : access is denied | |
* $> icacls %userprofile%\Appdata\LocalLow /t /setintegritylevel (OI)(CI)L | |
*/ | |
"use strict"; | |
// add object STORAGE to global namespace object AppHtml5 (changes these names if you like) | |
//but do not pollute global scope with some name like "storage", because it might already exists | |
window.AppHtml5 ? window.AppHtml5.storage = { "save" : {}, "load" : {}} : window.AppHtml5 = {"storage" : { "save" : {}, "load" : {}}}; | |
(function () { | |
////////////////////////// specific implementation | |
var handler, | |
webstorageHandler = { | |
_init : function(){ | |
return typeof (Storage) !== "undefined" ? true : false; | |
}, | |
save : function (key, val) { | |
localStorage.setItem(key, JSON.stringify(val)); | |
}, | |
load : function (key) { | |
return JSON.parse(localStorage.getItem(key)); | |
} | |
}, | |
cookieHandler = { | |
_init : function(){ | |
var cookiesEnabled = (function(){ | |
var id = new Date().getTime(); | |
document.cookie = '_cookieprobe' + id + ';path=/'; | |
return (document.cookie.indexOf(id) !== -1); | |
})(); | |
return cookiesEnabled; | |
}, | |
save : function (key, val) { | |
var exdays = 1, exdate = new Date(), jsonStr, escapedValue; | |
exdate.setDate(exdate.getDate() + exdays); | |
jsonStr = JSON.stringify(val); | |
escapedValue = escape(jsonStrval) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()); | |
document.cookie = key + "=" + escapedValue; | |
}, | |
load : function (key) { | |
var i, x, y, unescapedValue, ARRcookies = document.cookie.split(";"), jsonObj; | |
for (i = 0; i < ARRcookies.length; i++) { | |
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("=")); | |
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1); | |
x = x.replace(/^\s+|\s+$/g, ""); | |
if (x == key) { | |
unescapedValue = unescape(y); | |
jsonObj = JSON.parse(unescapedValue); | |
} | |
} | |
} | |
}; | |
//chose implementation based on what is available | |
if (webstorageHandler._init()) | |
handler = webstorageHandler; | |
else if (cookieHandler._init()) | |
handler = cookieHandler; | |
else | |
handler = { | |
"save" : new function(){ alert('No support for storage')}, | |
"load" : new function(){ alert('No support for storage')} | |
}; | |
//assign specific implementation to 'interface' in global namespace | |
window.AppHtml5.storage.save = function(key,value){ | |
handler.save(key,value); | |
} | |
window.AppHtml5.storage.load = function(key){ | |
return handler.load(key); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment