-
-
Save maartenJacobs/2711578 to your computer and use it in GitHub Desktop.
session / local storage switcher
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
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