Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
localStorage clear function with exceptions
clearStorage = function(exceptions){
var storage = localStorage
var keys = [];
var exceptions = [].concat(exceptions) //prevent undefined
//get storage keys
$.each(localStorage, function(key, val) {
keys.push(key);
});
//loop through keys
for( i=0; i<keys.length; i++ ){
var key = keys[i]
var deleteItem = true
//check if key excluded
for( j=0; j<exceptions.length; j++ ){
var exception = exceptions[j];
if( key == exception ) deleteItem = false;
}
//delete key
if( deleteItem ){
localStorage.removeItem(key)
}
}
}
@faisalfsl33

This comment has been minimized.

Copy link

@faisalfsl33 faisalfsl33 commented Dec 11, 2016

ty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment