当 localStorage 已满时会发生什么? [英] What happens when localStorage is full?

查看:93
本文介绍了当 localStorage 已满时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

  • 上面提到的浏览器会从我的网站上删除数据还是会选择最旧的"?或类似的东西?
  • 在这种情况下,我的项目会被保存吗?

还有最重要的:

  • 可以说我滥用"了我网站上的 localStorage 试图将其全部用完,在同一页面中我正在填充它并试图保存更多.我会收到警告吗?发生这种情况时 getItem 会返回 null 还是以某种方式保存在内存中?

  • Lets say I "abuse" the localStorage on 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? Will the getItem return null when this happens or is it somehow saved in memory?

如果我尝试保存大于 localStorage 大小的项目会怎样?

What happens if I try and save an item larger than the localStorage size?

回答:可以找到答案这里

  • 可以从 sessionStorage 中获得完全相同的行为吗?据称应该是相同的?

我知道这是很多问题,但我正在努力了解与该主题相关的所有内容,如果您能回答问题的任何部分,我将不胜感激.

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

推荐答案

首先,一些有用的资源:

Firstly, some useful resources:

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

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:

  • Opera:opera:config -> localStorage 的域配额
  • Firefox:about:config -> dom.storage.default_quota

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

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.

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

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

  • 存储数据,如果相同的键已存在,则覆盖值.

如果不是:

  • 不会存储数据,也不会覆盖现有数据.
  • 抛出一个 QUOTA_EXCEEDED_ERR 异常.

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

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

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

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

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

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

这篇关于当 localStorage 已满时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆