Html localStorage 什么时候清除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8537112/
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
When is localStorage cleared?
提问by Bnicholas
How long can I expect data to be kept in localStorage. How long will an average user's localStorage data persist? If the user doesn't clear it, will it last till a browser re-install?
我可以期望数据在 localStorage 中保留多长时间。普通用户的 localStorage 数据会持续多久?如果用户不清除它,它会持续到浏览器重新安装吗?
Is this consistent across browsers?
这在浏览器中是否一致?
采纳答案by Dominic Green
W3C draft says this
W3C 草案是这样说的
User agents should expire data from the local storage areas only for security reasons or when requested to do so by the user. User agents should always avoid deleting data while a script that could access that data is running.
用户代理应该仅出于安全原因或当用户要求这样做时才使本地存储区域中的数据过期。用户代理应始终避免在可以访问数据的脚本运行时删除数据。
So if browsers follow the spec it should persist untill the user removes it on all browsers, I have not found any that have deleted on any off my projects.
因此,如果浏览器遵循规范,它应该一直存在,直到用户在所有浏览器上删除它为止,我还没有发现任何已在我的项目中删除的内容。
A good article to read is also http://ejohn.org/blog/dom-storage/
一篇值得阅读的好文章也是http://ejohn.org/blog/dom-storage/
回答by cssyphus
localStorage is also known as Web Storage, HTML5 Storage, and DOM Storage (these all mean the same thing).
localStorage 也称为 Web 存储、HTML5 存储和 DOM 存储(这些都是同一个意思)。
localStorage is similar to sessionStorage, except thatdata stored in localStorage has no expiration time, while data stored in sessionStorage gets cleared when the browsing session ends (i.e. when the browser / browser tab is closed). Session storage is used much less often than localStorage, and exists only within the current browser tab - even two tabs loaded with the same website will have different sessionStorage data. sessionStorage data survives page refresh, but not closing/opening the tab. LocalStorage data, on the other hand, is shared between all tabs and windows from the same origin. LocalStorage data does not expire; it remains after the browser is restarted and even after OS reboot.Source
localStorage 与 sessionStorage 类似,不同之处在于localStorage 中存储的数据没有过期时间,而 sessionStorage 中存储的数据在浏览会话结束时(即浏览器/浏览器选项卡关闭时)被清除。会话存储的使用频率远低于 localStorage,并且仅存在于当前浏览器选项卡中——即使加载了同一个网站的两个选项卡也会有不同的 sessionStorage 数据。sessionStorage 数据在页面刷新后仍然存在,但不会关闭/打开选项卡。另一方面,LocalStorage 数据在来自同一来源的所有选项卡和窗口之间共享。LocalStorage 数据不会过期;它在浏览器重新启动甚至操作系统重新启动后仍然存在。来源
localStorage is available on all browsers, but persistence is not consistently implemented. In particular, localStorage can be cleared by user action and may be cleared inadvertently (who would think that clearing all cookies also clears localStorage?).
localStorage 在所有浏览器上都可用,但持久性并未一致实现。特别是 localStorage 可以通过用户操作清除,并且可能会被无意清除(谁会认为清除所有 cookie 也会清除 localStorage?)。
In Firefox, localStorage is cleared when these three conditions are met: (a) user clears recent history, (b) cookies are selected to be cleared, (c) time range is "Everything"
在 Firefox 中,满足这三个条件时会清除 localStorage:(a) 用户清除最近的历史记录,(b) 选择清除 cookie,(c) 时间范围为“一切”
In Chrome, localStorage is cleared when these conditions are met: (a) clear browsing data, (b) "cookies and other site data" is selected, (c) timeframe is "from beginning of time". In Chrome, it is also now possible to delete localStorage for one specific site.
在 Chrome 中,满足以下条件时会清除 localStorage:(a) 清除浏览数据,(b) 选择“cookies 和其他站点数据”,(c) 时间范围是“从时间开始”。在 Chrome 中,现在还可以删除特定站点的 localStorage。
In IE, to clear localStorage: (a) Tools--Internet Options, (b) General tab, (c) delete browsing history on exit, (d) ensure "Cookies and website data" (or "temporary internet files and website files") is selected, (e) consider unchecking "Preserve Favorites website data" at the top
在 IE 中,要清除 localStorage:(a) 工具--Internet 选项,(b) 常规选项卡,(c) 退出时删除浏览历史记录,(d) 确保“Cookie 和网站数据”(或“临时 Internet 文件和网站文件”) ") 被选中,(e) 考虑取消选中顶部的“保留收藏夹网站数据”
In Safari: (a) Click Safari (b) Preferences (c) Select the Privacy tab (d) Click Remove all website data (e) Click Remove Now
在 Safari 中:(a) 单击 Safari (b) 首选项 (c) 选择隐私选项卡 (d) 单击删除所有网站数据 (e) 单击立即删除
Opera: Despite excellent articles on localStorage from the Opera site, I haven't yet found clear (non-programmatic) instructions to users on how to clear localStorage. If anyone finds, please leave a comment below this answer with reference link.
Opera:尽管 Opera 站点上有关于 localStorage 的优秀文章,但我还没有找到关于如何清除 localStorage 的明确(非编程)用户说明。如果有人发现,请在此答案下方留言并附上参考链接。
The Opera dev sitehas an excellent summary of localStorage:
该歌剧开发站点具有的localStorage的一个很好的总结:
The current way of storing data on the client-side — cookies — is a problem:
Low size: Cookies generally have a maximum size of around 4 KB, which is not much good for storing any kind of complex data
It's difficult for cookies to keep track of two or more transactions on the same site, which might be happening in two or more different tabs
Cookies can be exploited using techniques such as cross site scripting, resulting in security breaches
Other (less popular) alternatives to cookies include techniques involving query strings, hidden form fields, flash based local shared objects, etc. Each with their own set of problems related to security, ease of use, size restrictions etc. So up until now we have been using pretty bad ways of storing data on the user's end. We need a better way, which is where Web Storage comes in.
Web Storage
The W3C Web Storage specification was designed as a better way of storing data on the client-side. It has two different types of storage: Session Storage and Local Storage.
Both Session and Local Storage will typically be able to store around 5 MB of data per domain, which is significantly more than cookies.
当前在客户端存储数据的方式——cookies——是一个问题:
体积小:Cookies的最大大小一般在4KB左右,对于存储任何类型的复杂数据都不是很好
cookie 很难跟踪同一站点上的两个或多个交易,这可能发生在两个或多个不同的选项卡中
可以使用跨站点脚本等技术来利用 Cookie,从而导致安全漏洞
cookie 的其他(不太流行的)替代方案包括涉及查询字符串、隐藏表单字段、基于 Flash 的本地共享对象等的技术。每个都有自己的一组与安全性、易用性、大小限制等相关的问题。所以到目前为止,我们一直在使用非常糟糕的方式在用户端存储数据。我们需要一种更好的方法,这就是 Web Storage 的用武之地。
网络存储
W3C Web Storage 规范被设计为一种在客户端存储数据的更好方式。它有两种不同类型的存储:会话存储和本地存储。
会话和本地存储通常每个域都能够存储大约 5 MB 的数据,这比 cookie 多得多。
Resources:
资源:
https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
https://javascript.info/localstorage
https://javascript.info/localstorage
https://dev.opera.com/articles/web-storage/
https://dev.opera.com/articles/web-storage/
http://www.quirksmode.org/html5/storage.html
http://www.quirksmode.org/html5/storage.html
http://www.ghacks.net/2015/02/05/how-to-clear-web-storage-in-your-browser-of-choice/
http://www.ghacks.net/2015/02/05/how-to-clear-web-storage-in-your-browser-of-choice/
http://www.opera.com/dragonfly/documentation/storage/
http://www.opera.com/dragonfly/documentation/storage/
DOMStorage article on MDN (written by John Resig)
MDN 上的 DOMStorage 文章(由 John Resig 撰写)
回答by Fabrizio Calderan
The content in localstorage is persistent as long as the user chooses to clear the storage (entirely or a single value inside it)
只要用户选择清除存储(整个或其中的单个值),localstorage 中的内容就是持久化的
About the consistency across browser, localstorage is currently available on every major browser, including IE8+ (see http://caniuse.com/#feat=namevalue-storage)
关于跨浏览器的一致性,localstorage 目前在包括 IE8+ 在内的所有主流浏览器上都可用(参见http://caniuse.com/#feat=namevalue-storage)
回答by Uday Sravan K
In Chrome while performing 'clear browsing data' , if you choose 'Cookies and other site and plugin data' option then sessionStorage data will be erased.
在 Chrome 中执行“清除浏览数据”时,如果您选择“Cookie 和其他站点和插件数据”选项,则 sessionStorage 数据将被删除。