Html 子域的 HTML5 localStorage 大小限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2747285/
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
HTML5 localStorage size limit for subdomains
提问by Chuck
HTML5's localStorage databases are usually size-limited — standard sizes are 5 or 10 MB per domain. Can these limits be circumvented by subdomains (e.g. example.com, hack1.example.com and hack2.example.com all have their own 5 MB databases)? And is there anything in the standard that specifies whether parent domains can access their children's databases? I can't find anything, and I can see arguments for doing it either way, but it seems like there has to be some standard model.
HTML5 的 localStorage 数据库通常有大小限制——标准大小为每个域 5 或 10 MB。子域可以绕过这些限制吗(例如,example.com、hack1.example.com 和 hack2.example.com 都有自己的 5 MB 数据库)?标准中是否有任何内容指定父域是否可以访问其子域的数据库?我找不到任何东西,我可以看到任何一种方式的论据,但似乎必须有一些标准模型。
采纳答案by Vetle
From http://dev.w3.org/html5/webstorage/#disk-space
来自http://dev.w3.org/html5/webstorage/#disk-space
A mostly arbitrary limit of five megabytes per origin is recommended. Implementation feedback is welcome and will be used to update this suggestion in the future.
建议对每个源的大多数任意限制为 5 兆字节。欢迎实施反馈,并将用于在未来更新此建议。
It also mentions that :
它还提到:
User agents should guard against sites storing data under the origins other affiliated sites, e.g. storing up to the limit in a1.example.com, a2.example.com, a3.example.com, etc, circumventing the main example.com storage limit.
用户代理应防止站点将数据存储在其他附属站点的来源下,例如在 a1.example.com、a2.example.com、a3.example.com 等中存储最多限制,绕过主要的 example.com 存储限制.
回答by J?rn Zaefferer
Here's a pretty detailed test result with plenty of desktop and mobile browsers covered: http://dev-test.nemikor.com/web-storage/support-test/
这是一个非常详细的测试结果,涵盖了大量桌面和移动浏览器:http: //dev-test.nemikor.com/web-storage/support-test/
Which confirms this bug report: http://code.google.com/p/chromium/issues/detail?id=58985#c15
这证实了这个错误报告:http: //code.google.com/p/chromium/issues/detail?id=58985#c15
You can rely on only 2.5MB, not 5MB, based on the string length that you can store.
根据您可以存储的字符串长度,您只能依赖 2.5MB,而不是 5MB。
回答by Kevin Hakanson
I missed this question when I asked "Is 5MB the de facto limit for W3C Web Storage?", but I got basically the same answer. If you want more information, I did link to some browser specific limits in my question.
当我问“ W3C Web Storage 的实际限制是 5MB 吗?”时,我错过了这个问题,但我得到的答案基本相同。如果您需要更多信息,我确实在我的问题中链接到了某些浏览器特定限制。
回答by Clay Nichols
A better solution is to use the [HTML5 IndexedDB for offline storage.]1
更好的解决方案是使用 [HTML5 IndexedDB 进行离线存储。] 1
It looks like the replacement for the old Web SQL (which seems to be misnamed b/c it's for offlinestorage) is: Indexed DB, which allows offline storage and is still supportd:
看起来旧 Web SQL 的替代品(似乎被错误命名为 b/c 用于离线存储)是: 索引 DB,它允许离线存储并且仍然受支持:
IndexedDB is new in HTML5. Web Databases are hosted and persisted inside a user's browser. By allowing developers to create applications with rich query abilities it is envisioned that a new breed of web applications will emerge that have the ability to work online and off-line.
IndexedDB 是 HTML5 中的新内容。Web 数据库托管并持久保存在用户的浏览器中。通过允许开发人员创建具有丰富查询能力的应用程序,可以预见将出现具有在线和离线工作能力的新型 Web 应用程序 。
More info and a test-appat: http://ido-green.appspot.com/WebSQL-IndexedDB-example/jqm_indexedDB.html
更多信息和测试应用程序:http: //ido-green.appspot.com/WebSQL-IndexedDB-example/jqm_indexedDB.html
回答by Pawel
To get 50MB of storage space use code below
要获得 50MB 的存储空间,请使用以下代码
// 1. paste this line in your code
!function(){function e(t,o){return n?void(n.transaction("s").objectStore("s").get(t).onsuccess=function(e){var t=e.target.result&&e.target.result.v||null;o(t)}):void setTimeout(function(){e(t,o)},100)}var t=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB;if(!t)return void console.error("indexDB not supported");var n,o={k:"",v:""},r=t.open("d2",1);r.onsuccess=function(e){n=this.result},r.onerror=function(e){console.error("indexedDB request error"),console.log(e)},r.onupgradeneeded=function(e){n=null;var t=e.target.result.createObjectStore("s",{keyPath:"k"});t.transaction.oncomplete=function(e){n=e.target.db}},window.ldb={get:e,set:function(e,t){o.k=e,o.v=t,n.transaction("s","readwrite").objectStore("s").put(o)}}}();
// 2. Setting values
ldb.set('nameGoesHere', 'value goes here');
// 3. Getting values - callback is required because the data is being retrieved asynchronously:
ldb.get('nameGoesHere', function (value) {
console.log('And the value is', value);
});