Html 当 localStorage 已满时会发生什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13567509/
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-08-29 04:05:32  来源:igfitidea点击:

What happens when localStorage is full?

htmlfirefoxgoogle-chromesafarilocal-storage

提问by eric.itzhak

I have found articlesregarding cache behaviour so i can only assume that it's not much different but i wanted to make sure.

我找到有关缓存行为的文章,所以我只能假设它没有太大不同,但我想确定一下。

I have read that most browser have 5MB (give or take) for localStorage and i was intrested in what would be the behaviour of the browsers?

我读到大多数浏览器都有 5MB(给予或接受)用于 localStorage,我对浏览器的行为很感兴趣?

I understand every browser acts diffrently but i'm intrested mostly in Safari, Chrome and Firefox (as those are the ones i consider as browsers).

我了解每个浏览器的行为都不同,但我最感兴趣的是 Safari、Chrome 和 Firefox(因为我认为这些浏览器是浏览器)。

  • Will the browsers mentioned above delete data from my website or it will choose "the oldest" or something of the sort?
  • Will my item be saved in such case?
  • 上面提到的浏览器会从我的网站上删除数据还是会选择“最旧的”或类似的东西?
  • 在这种情况下,我的物品会被保存吗?

And the most important :

而最重要的是:

  • Lets say i "abuse" the localStorage and my website trying to use it all up, and in the same page i'm filling it up and trying to save more, will i get a warning ANDwill the getItemreturn null when this happens or it somehow saved in memory?

  • What happens if i try and save an item larger then the localStorage size?

    Answered :answer can be found here

  • Is the same exact behaviour can be expected from sessionStorage which allegdly should be the same?

  • 可以说,我“虐待”的localStorage的和我的网站尝试使用这一切,并在同一页我暂它,并试图挽救更多的,我会得到一个警告意志的getItem回空当这种情况发生,或者以某种方式保存在内存中?

  • 如果我尝试保存一个大于 localStorage 大小的项目会发生什么?

    回答:可以在这里找到答案

  • sessionStorage 是否可以预期完全相同的行为,据称应该相同?

I know this is alot of questions but i'm trying to understand all that's related to the subject, i'd be thankfull for any part of the question you can answer.

我知道这是很多问题,但我正在尝试了解与该主题相关的所有内容,对于您可以回答的问题的任何部分,我将不胜感激。

Regards.

问候。

回答by tagawa

Firstly, some useful resources:

首先,一些有用的资源:

In answer to your question, desktop browsers tend to have an initial maximum localStorage quota of 5MB per domain. This can be adjusted by the user in some cases:

在回答您的问题时,桌面浏览器的初始最大 localStorage 配额往往为每个域 5MB。在某些情况下,这可以由用户调整:

  • Opera: opera:config -> Domain Quota For localStorage
  • Firefox: about:config -> dom.storage.default_quota
  • Opera:opera:config -> localStorage 的域配额
  • 火狐:about:config -> dom.storage.default_quota

In Chrome, there doesn't seem to be a way for the user to adjust this setting although like Opera, localStorage data can be edited directly per domain using the Developer Tools.

在 Chrome 中,用户似乎没有办法调整此设置,尽管与 Opera 一样,可以使用开发人员工具直接按域编辑 localStorage 数据。

When you try to store data in localStorage, the browser checks whether there's enough remaining space for the current domain. If yes:

当您尝试在 localStorage 中存储数据时,浏览器会检查当前域是否有足够的剩余空间。如果是:

  • The data is stored, overwriting values if an identical key already exists.
  • 数据被存储,如果相同的键已经存在,则覆盖值。

If no:

如果不:

  • The data is not stored and no existing data is overwritten.
  • A QUOTA_EXCEEDED_ERRexception is thrown.
  • 不会存储数据,也不会覆盖现有数据。
  • 一个QUOTA_EXCEEDED_ERR异常被抛出。

In this case, getItem(key)will return the last value that was successfully stored, if any.

在这种情况下,getItem(key)将返回成功存储的最后一个值(如果有)。

(Opera is slightly different in that it displays a dialog box giving the user the choice of increasing storage space for the current domain.)

(Opera 略有不同,它显示一个对话框,让用户可以选择增加当前域的存储空间。)

Note that sessionStorage and localStorage are both implementations of the same Storage object so their behaviour is similar and error handling is the same.

请注意 sessionStorage 和 localStorage 都是同一个 Storage 对象的实现,所以它们的行为是相似的,错误处理是相同的。