Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

html - What happens when localStorage is full?

I have found articles regarding 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 interested in what would be the behaviour of the browsers?

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).

  • 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 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?

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

Answered: answer can be found here

  • Can the same exact behaviour be expected from sessionStorage which allegedly should be the same?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  • Opera: opera:config -> Domain Quota For localStorage
  • Firefox: 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.

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

  • 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_ERR exception is thrown.

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...