Skip to content

Instantly share code, notes, and snippets.

@maartenJacobs
Forked from stugoo/gist:2711536
Created May 16, 2012 15:56
Show Gist options
  • Save maartenJacobs/2711578 to your computer and use it in GitHub Desktop.
Save maartenJacobs/2711578 to your computer and use it in GitHub Desktop.
session / local storage switcher
var shortlistings = (function() {
"use strict";
var exports = {
disableSession: disableSession,
enableSession: enableSession,
set: set
},
session_storage_method = window.sessionStorage,
local_storage_method = window.localStorage,
save_method = session_storage_method;
function disableSession() {
save_method = local_storage_method;
// Do switch code here ...
}
function enableSession() {
save_method = session_storage_method;
// Do switch code here
}
/**
* Saves a key to value map, and returns state of success.
*/
function set(type, data) {
return save_method.setItem(type, JSON.stringify(data));
}
/**
* Returns a value, by the key to which it was stored.
*/
function get(type) {
return save_method.getItem(type);
}
// Your code here,
// Refactor out jQuery please!
function sessionManagement() {
$('input[name=cookieSettings]').change(function() {
var radio = $('input[name=cookieSettings]:checked').val();
if ((setting === 'permanent') || (setting === undefined)) {
shortlistBar.storeInSession = false; // localStorage
} else if ((setting === 'session') || (setting === undefined)) {
shortlistBar.storeInSession = true; // sessionStorage
} else if ((radio === 'off') ) {
if (confirm('are you sure you wish to delete all cookies/storage?')) {
sessionStorage.clear();
localStorage.clear(); // empty storage
shortlistBar.killFunctions(); // kill all the functions!
}
}
});
}
return exports;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment