HTML5 localStorage size limit for subdomains

假装没事ソ 提交于 2019-11-26 04:47:06

问题


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.


回答1:


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

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.




回答2:


Here's a pretty detailed test result with plenty of desktop and mobile browsers covered: 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

You can rely on only 2.5MB, not 5MB, based on the string length that you can store.




回答3:


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.




回答4:


A better solution is to use the [HTML5 IndexedDB for offline storage.]1

It looks like the replacement for the old Web SQL (which seems to be misnamed b/c it's for offline storage) is: Indexed DB, which allows offline storage and is still supportd:

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.

More info and a test-app at: http://ido-green.appspot.com/WebSQL-IndexedDB-example/jqm_indexedDB.html




回答5:


To get 50MB of storage space use code below

// 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);
});

source https://github.com/DVLP/localStorageDB



来源:https://stackoverflow.com/questions/2747285/html5-localstorage-size-limit-for-subdomains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!