有没有办法一次性删除localStorage的全部内容?

发布于 2024-11-26 01:06:50 字数 328 浏览 1 评论 0原文

我有一个网络应用程序,可以将用户页面历史记录填充到本地存储中。咳咳。它应该是sessionStorage,我现在已经更改了它,以便它不会持续存在。

但这给我留下了一个有趣的问题:一个完整​​的本地存储被清空。我知道您可以在开发人员控制台中编辑存储来删除数据,但想象一下我出于某种原因想要使用 localStorage,并且在某些时候我想完全删除以编程方式放置在那里的数据内容。

我知道本地存储是特定于域的,所以这不应该成为问题 - 但我的问题是如何全局识别我的数据然后删除它?

是否有一个 js 表达式可以一次性删除我的数据?或者我是否坚持使用密钥的命名约定,然后使用正则表达式来选择所有密钥?

I have a web app that populates user page history into the local storage. Ahem. It should be sessionStorage, which I have now changed so that it doesn't persist.

But that left me with an interesting problem: a full localStorage to be emptied. I know you can edit the storage in the developer console to remove the data, but imagine I wanted to use localStorage for whatever reason, and at some point I wanted to completely delete the contents of data that I had placed there programmatically.

I understand local storage is domain specific so this should not be a problem - but my question is how do I globally identify my data and then delete it?

Is there a single js expression that I could use that will just wipe out my data in one hit? Or am I stuck with using naming conventions for my key and then using a reg-ex to select all the keys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2024-12-03 01:06:50

就像这样简单:

localStorage.clear();

规范:http://dev.w3.org/html5/ webstorage/#dom-storage-clear

As easy as:

localStorage.clear();

Spec: http://dev.w3.org/html5/webstorage/#dom-storage-clear

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文