Skip to content

Instantly share code, notes, and snippets.

@christianjuth
Created April 4, 2015 19:09
Show Gist options
  • Save christianjuth/8161d1161668e021a580 to your computer and use it in GitHub Desktop.
Save christianjuth/8161d1161668e021a580 to your computer and use it in GitHub Desktop.
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
Copy link

ty

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