简体   繁体   English

如何在javascript中清除localstorage,sessionStorage和cookies?然后检索?

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

How to completely clear localstorage , sessionStorage and cookies in javascript ? 如何在javascript中完全清除localstoragesessionStoragecookies

Is there any way one can get these values back after clearing them ? 清除它们之后有什么方法可以获得这些值吗?

how to completely clear 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. 但如果这与安全问题有关,则不应该依赖于此。

There is no way to retrieve localStorage, sessionStorage or cookie values via javascript in the browser after they've been deleted via javascript. 在通过javascript删除后,无法通过浏览器中的javascript检索localStorage,sessionStorage或cookie值。

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. 例如,Firefox将cookie存储为单个文件。 When a cookie is deleted, its file is deleted. 删除cookie时,将删除其文件。 That means that the cookie can no longer be accessed via the browser. 这意味着无法再通过浏览器访问cookie。 But, we know that from outside the browser, using system tools, the contents of deleted files can sometimes be retrieved. 但是,我们知道从浏览器外部使用系统工具,有时可以检索已删除文件的内容。

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

The standard Web Storage , does not say anything about the restoring any of these. 标准Web存储 ,没有说明恢复任何这些。 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. 您必须完成浏览器实现这些操作的方式,或者在删除它们之前找到备份这些方法的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM