Created
April 4, 2015 19:09
-
-
Save christianjuth/8161d1161668e021a580 to your computer and use it in GitHub Desktop.
localStorage clear function with exceptions
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
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ty