本文翻译自:Clearing localStorage in javascript?
有没有办法在javascript中重置/清除浏览器的localStorage?
#1楼
参考:https://stackoom.com/question/WAmk/在javascript中清除localStorage
#2楼
如果要从用户的本地存储中删除特定的项目或变量,可以使用
localStorage.removeItem("name of localStorage variable you want to remove");
#3楼
Here is a function that will allow you to remove all localStorage items with exceptions. 这是一个允许您删除所有localStorage项目的函数。 You will need jQuery for this function. 你需要jQuery来实现这个功能。 You can download the gist . 你可以下载要点 。
You can call it like this 你可以这样称呼它
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);
}
}
};
#4楼
First things first, you need to check to make sure that localStorage is enabled. 首先,您需要检查以确保启用了localStorage。 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. 是的,您可以(在某些情况下)检查localStorage是否是窗口对象的成员。 However, there are iframe sandboxing options (among other things) that will throw an exception if you even attempt to access the index 'localStorage'. 但是,如果您甚至尝试访问索引“localStorage”,则会有iframe沙盒选项(以及其他内容)抛出异常。 Thus, for best-practices reasons, this is the best way to check to see if the localStorage is enabled. 因此,出于最佳实践原因,这是检查localStorage是否已启用的最佳方法。 Then, you can just clear the localStorage like so. 然后,您可以像这样清除localStorage。
if (localStorageEnabled) localStorage.clear();
For example, you could clear the localStorage after an error occurs in webkit browsers like so. 例如,您可以在webkit浏览器中发生错误后清除localStorage。
// clears the local storage upon error
if (localStorageEnabled)
window.onerror = localStorage.clear.bind(localStorage);
In the above example, you need the .bind(window)
because without it, the localStorage.clear
function will run in the context of the window
object, instead of the localStorage
object making it silently fail. 在上面的示例中,您需要.bind(window)
因为没有它, localStorage.clear
函数将在window
对象的上下文中运行,而不是localStorage
对象使其无声地失败。 To demonstrate this, look at the below example: 为了证明这一点,请看下面的例子:
window.onerror = localStorage.clear;
is the same as: 是相同的:
window.onerror = function(){
localStorage.clear.call(window);
}
#5楼
localStorage.clear();
or 要么
window.localStorage.clear();
to clear particular item 清除特定项目
window.localStorage.removeItem("item_name");
To remove particular value by id : 要通过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;
}
});
#6楼
Localstorage is attached on the global window
. Localstorage附加在全局window
。 When we log localstorage in the chrome devtools we see that it has the following APIs: 当我们在chrome devtools中记录localstorage时,我们看到它有以下API:
We can use the following API's for deleting items: 我们可以使用以下API来删除项目:
-
localStorage.clear()
: Clears the whole localstoragelocalStorage.clear()
:清除整个localstorage -
localStorage.removeItem('myItem')
: To remove individual itemslocalStorage.removeItem('myItem')
:删除单个项目