javascript 如何清除javascript中的localstorage、sessionStorage和cookies?然后检索?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15804462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:09:44  来源:igfitidea点击:

how to clear localstorage,sessionStorage and cookies in javascript? and then retrieve?

javascript

提问by Gowsikan

How to completely clear localstorage, sessionStorageand cookiesin javascript ?

如何完全清除localstoragesessionStoragecookies在 javascript 中?

Is there any way one can get these values back after clearing them ?

有什么办法可以在清除这些值后取回它们吗?

回答by Halim Qarroum

how to completely clear localstorage

如何彻底清除localstorage

localStorage.clear();

how to completely clear sessionstorage

如何彻底清除sessionstorage

sessionStorage.clear();

[...] Cookies ?

[...] 饼干 ?

var cookies = document.cookie;

for (var i = 0; i < cookies.split(";").length; ++i)
{
    var myCookie = cookies[i];
    var pos = myCookie.indexOf("=");
    var name = pos > -1 ? myCookie.substr(0, pos) : myCookie;
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}

is there any way to get the value back after clear these ?

清除这些后有什么方法可以取回价值吗?

No, there isn't. But you shouldn't rely on this if this is related to a security question.

,没有。但是,如果这与安全问题有关,则不应依赖于此。

回答by jfriend00

There is no way to retrieve localStorage, sessionStorage or cookie values via javascript in the browser after they've been deleted via javascript.

在通过 javascript 删除 localStorage、sessionStorage 或 cookie 值后,无法在浏览器中通过 javascript 检索它们。

If what you're really asking is if there is some other way (from outside the browser) to recover that data, that's a different question and the answer will entirely depend upon the specific browser and how it implements the storage of each of those types of data.

如果您真正要问的是是否有其他方式(从浏览器外部)来恢复该数据,这是一个不同的问题,答案将完全取决于特定的浏览器以及它如何实现每种类型的存储数据的。

For example, Firefox stores cookies as individual files. When a cookie is deleted, its file is deleted. That means that the cookie can no longer be accessed via the browser. But, we know that from outside the browser, using system tools, the contents of deleted files can sometimes be retrieved.

例如,Firefox 将 cookie 存储为单独的文件。当 cookie 被删除时,它的文件也被删除。这意味着无法再通过浏览器访问 cookie。但是,我们知道,从浏览器外部,使用系统工具,有时可以检索已删除文件的内容。

If you wanted to look into this further, you'd have to discover how each browser stores each data type on each platform of interest and then explore if that type of storage has any recovery strategy.

如果您想进一步研究这一点,您必须了解每个浏览器如何在每个感兴趣的平台上存储每种数据类型,然后探索该类型的存储是否有任何恢复策略。

回答by Kishore

The standard Web Storage, does not say anything about the restoring any of these. So there won't be any standard way to do it. You have to go through the way the browsers implement these, or find a way to backup these before you delete them.

标准的Web Storage,没有说明任何关于恢复这些的任何内容。所以不会有任何标准的方法来做到这一点。您必须通过浏览器实现这些的方式,或者在删除它们之前找到备份它们的方法。