简体   繁体   English

浏览器如何删除不再使用的本地存储?

[英]How do Browsers delete local storage no longer in use?

According to MDN a web page's local storage is kept by the browser itself. 根据MDN,网页的本地存储由浏览器本身保存。

  1. How does the browser associate a web page with the page's allocated local storage? 浏览器如何将网页与页面分配的本地存储相关联?

    I wrote a web page that uses local storage for personal use. 我写了一个使用本地存储供个人使用的网页。 In development I have multiple versions that all added to local storage. 在开发中,我有多个版本都添加到本地存储中。

    If I delete a web page that employs local storage then how does the browser know to delete the local storage for that web page? 如果我删除使用本地存储的网页,那么浏览器如何知道要删除该网页的本地存储?

  2. (My main question) I am worried that local storage kept by a browser will continue to grow and grow if I repeatedly delete web pages that employ local storage. (我的主要问题)我担心如果我反复删除使用本地存储的网页,浏览器保存的本地存储会继续增长。

Local storage is stored in relation to the calling web page's domain, not individual pages. 本地存储是相对于调用网页的域而不是单个页面来存储的。 So adding to local storage via foo.com/a , foo.com/b and foo.com/c will all contribute to the browser's allocation for foo.com . 因此,通过增加本地存储foo.com/afoo.com/bfoo.com/c都将有助于为浏览器的配置foo.com

The browser deletes localStorage data when requested by the calling page, or when cleared out manually by the user. 当调用页面请求或用户手动清除时,浏览器将删除localStorage数据。 Unlike cookies or sessions localStorage does not automatically expire data. 与cookie或会话不同,localStorage不会自动使数据过期。 So scripts using localStorage should tidy up after themselves where possible, and watch out for hitting the allowed limit (for example using try/catch to avoid an QUOTA_EXCEEDED_ERR exception when using localStorage.setItem ). 因此,使用localStorage的脚本应在可能的情况下整理自己,并注意达到允许的限制(例如,在使用localStorage.setItem时,使用try/catch以避免QUOTA_EXCEEDED_ERR异常)。

Thanks Graham! 谢谢格雷厄姆! From perusing Stack Overflow, Ctrl-Shift-i will bring up local storage information on the current web page's domain. 通过仔细阅读Stack Overflow,Ctrl-Shift-i将在当前网页的域上显示本地存储信息。 What I discovered is that local web pages I create myself on my computer all have the domain name "file://". 我发现我在计算机上创建的本地网页都具有域名“ file://”。

Since all local web applications I create on my computer have the same domain name, clearing local storage from one is bad because it clears it from all the others. 由于我在计算机上创建的所有本地Web应用程序都具有相同的域名,因此从其中一个清除本地存储是很不好的,因为它会从其他所有存储中清除本地存储。 This means that different applications shouldn't use the say keys and that each application should clear its own keys rather than the entire local storage. 这意味着不同的应用程序不应使用say键,并且每个应用程序应清除其自己的键而不是整个本地存储。 Anyway you were dead on. 无论如何,你死定了。

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

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