Created
August 10, 2012 05:34
-
-
Save c4y/3311337 to your computer and use it in GitHub Desktop.
sessionStorage
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
// Initialize | |
var sds = new SessionDataStore(); | |
// Store simple string | |
sds.set('foo', 'bar'); | |
// Store object | |
sds.set('testobject', { foo: "bar" } ); | |
// Get value back (will be a proper object, not just string) | |
var val = sds.get('testobject'); | |
// Copy something | |
sds.copy('foo', 'foo2'); | |
// Rename things | |
sds.rename('foo2', 'foo3'); | |
// Check for something | |
if (sds.has('foo3')) { ... } | |
// Remove | |
sds.remove('foo3'); | |
// Clear all | |
sds.clear(); | |
// Base data store | |
function BaseDataStore() | |
{ | |
// Private property for storage type | |
var storageType = ''; | |
// Set storage type | |
this.setStorageType = function(type) | |
{ | |
storageType = type; | |
} | |
// Get storage type | |
this.getStorageType = function() | |
{ | |
return storageType; | |
} | |
// Private method to check HTML5 LS/SS | |
var checkHtml5 = function() | |
{ | |
var ok = ('sessionStorage' in window && window['sessionStorage'] !== null && | |
'localStorage' in window && window['localStorage'] !== null); | |
if (!ok) | |
{ | |
throw 'Error: sessionStorage and/or localStorage are not supported.'; | |
} | |
} | |
// Public method to store key/value | |
this.set = function(key, value) | |
{ | |
checkHtml5(); | |
// Always serialize as JSON | |
value = JSON.stringify(value); | |
// Store in SS or LS | |
if (storageType == 'session') | |
{ | |
window.sessionStorage.setItem(key, value); | |
} | |
else | |
{ | |
window.localStorage.setItem(key, value); | |
} | |
} | |
// Public method to get value by key | |
this.get = function(key) | |
{ | |
checkHtml5(); | |
// Retrieve from SS or LS | |
if (storageType == 'session') | |
{ | |
var val = window.sessionStorage.getItem(key); | |
} | |
else | |
{ | |
var val = window.localStorage.getItem(key); | |
} | |
return eval('(' + val + ')'); | |
} | |
// Public method to wipe LS/SS | |
this.clear = function() | |
{ | |
checkHtml5(); | |
if (storageType == 'session') | |
{ | |
window.sessionStorage.clear(); | |
} | |
else | |
{ | |
window.localStorage.clear(); | |
} | |
} | |
// Check if given key exists | |
this.has = function(key) | |
{ | |
checkHtml5(); | |
if (storageType == 'session') | |
{ | |
var value = window.sessionStorage.getItem(key); | |
} | |
else | |
{ | |
var value = window.localStorage.getItem(key); | |
} | |
value = eval('(' + value + ')'); | |
return (value != null && value != undefined); | |
} | |
// Copies the value of the first key to the second key. | |
this.copy = function(source, target) | |
{ | |
var value = this.get(source); | |
this.set(target, value); | |
} | |
// Renames the first key to the second key. | |
this.rename = function(source, target) | |
{ | |
var value = this.get(source); | |
this.remove(source); | |
this.set(target, value); | |
} | |
// Removes the given key/value, from HTML5 localStorage if available, | |
// otherwise from a cookie. | |
this.remove = function(key) | |
{ | |
checkHtml5(); | |
if (storageType == 'session') | |
{ | |
window.sessionStorage.removeItem(key); | |
} | |
else | |
{ | |
window.localStorage.removeItem(key); | |
} | |
} | |
} | |
// Session storage data store | |
SessionDataStore.prototype = new BaseDataStore; | |
SessionDataStore.prototype.constructor = SessionDataStore; | |
function SessionDataStore() { | |
this.setStorageType('session'); | |
} | |
// Locale storage data store | |
LocalDataStore.prototype = new BaseDataStore; | |
LocalDataStore.prototype.constructor = LocalDataStore; | |
function LocalDataStore() { | |
this.setStorageType('local'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment