This article was translated from: Clearing localStorage in javascript?
Is there a way to reset / clear the browser's localStorage in javascript?
#1st Floor
Reference: https://stackoom.com/question/WAmk/ clear localStorage in javascript
#2nd Floor
If you want to delete a specific item or variable from the user's local storage, you can use
localStorage.removeItem("name of localStorage variable you want to remove");
#3rd floor
Here is a function that will allow you to remove all localStorage items with exceptions. This is a function that allows you to delete all localStorage items. By Will need by You jQuery for the this function. You need jQuery to achieve this function. CAN by You download at The GIST . You can download points .
You can call it like this you can call it
let clearStorageExcept = function(exceptions) {
let keys = [];
exceptions = [].concat(exceptions); // prevent undefined
// get storage keys
$.each(localStorage, (key) => {
keys.push(key);
});
// loop through keys
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let deleteItem = true;
// check if key excluded
for (let j = 0; j < exceptions.length; j++) {
let exception = exceptions[j];
if (key == exception) {
deleteItem = false;
}
}
// delete key
if (deleteItem) {
localStorage.removeItem(key);
}
}
};
#4th floor
First things first, you need to check to make sure that localStorage is enabled. First , you need to check to make sure that localStorage is enabled . I would recommend doing it like this: I would recommend doing it like this :
var localStorageEnabled = false;
try { localStorageEnabled = !!localStorage; } catch(e) {};
Yes, you can (in some cases) just check to see if the localStorage is a member of the window object. Yes, you can (in some cases) check whether localStorage is a member of the window object . However, there are iframe sandboxing options (among other things) that will throw an exception if you even attempt to access the index 'localStorage'. However, if you even try to access the index "localStorage", there will be an iframe sandbox option (and Other content) throws an exception. Thus, for best-practices reasons, this is the best way to check to see if the localStorage is enabled. Therefore, for best practice reasons, this is the best way to check whether localStorage is enabled. Then, you can just clear the localStorage like so. Then, you can clear localStorage like this.
if (localStorageEnabled) localStorage.clear();
For example, you could clear the localStorage after an error occurs in webkit browsers like so. For example, you can clear localStorage after an error occurs in the webkit browser.
// clears the local storage upon error
if (localStorageEnabled)
window.onerror = localStorage.clear.bind(localStorage);
The In The above Example, you need The .bind(window)
Because the without IT, The localStorage.clear
function Will RUN in The context of The window
Object, INSTEAD of The localStorage
Object Making IT silently Fail. In the above example, you need .bind(window)
because without it, localStorage.clear
the function will be in window
the context object The localStorage
object is running instead of making it fail silently. To demonstrate this, look at the below example: To prove this, please see the following example:
window.onerror = localStorage.clear;
is the same as: is the same as :
window.onerror = function(){
localStorage.clear.call(window);
}
#5th Floor
localStorage.clear();
or either
window.localStorage.clear();
to clear particular item remove specific items
window.localStorage.removeItem("item_name");
To remove particular value by id: To remove a specific value by id:
var item_detail = JSON.parse(localStorage.getItem("key_name")) || [];
$.each(item_detail, function(index, obj){
if (key_id == data('key')) {
item_detail.splice(index,1);
localStorage["key_name"] = JSON.stringify(item_detail);
return false;
}
});
#6th floor
Attached at The IS ON, Ltd. Free Join localStorage window
. LocalStorage additional globally window
. When we log localstorage in the chrome devtools we see that it has the following APIs: When we log localstorage in chrome devtools, we see that it has the following API:
We can use the following API's for deleting items: We can use the following API's for deleting items :
-
localStorage.clear()
: Clears the whole localstoragelocalStorage.clear()
:清除整个localstorage -
localStorage.removeItem('myItem')
: To remove individual itemslocalStorage.removeItem('myItem')
: Remove individual items