简体   繁体   English

HTML5离线缓存数据库存储

[英]HTML5 offline caching database storage

Im working on an HTML5 offline app im using a json string as my database 我正在使用json字符串作为数据库来处理HTML5离线应用程序

 [
            {"id":"1","data":"a","group_name":"erwr"},
            {"id":"2","data":"b","group_name":"dasd"},                        
            {"id":"3","data":"c","group_name":"yjg"},         
             -------------------------------
             -------------------------------
                 // it has around 3000 elements
    ]

Is it a good idea to store this entire json in a variable and assign like 将整个json存储在变量中并像

var mydatabase = "the above json";
localStorage.setItem('mydb', JSON.stringify(mydatabase ));

I think there are some limitations with the maximum number of characters for a var in javascript.so that the possibility of missing some elements in the json. 我认为javascript.var中的最大字符数有一些限制,因此可能会丢失json中的某些元素。

Another alternative that im using is put the entire thing in a hidden div 即时通讯使用的另一种选择是将整个东西放在一个隐藏的div中

<div id="db" style="display:none">/**the above json**/</div>

and accessing like 并像访问

var mydb = $('#db').html();
localStorage.setItem('mydb',mydb);

My question is that the second method an optimal one ? 我的问题是第二种方法是最佳方法吗? if not someone please provide another optimal way. 如果没有人,请提供另一种最佳方式。

The localStoage method is fine. localStoage方法很好。 Most browsers support at least 5 MB for localStorage and your data rows look small. 大多数浏览器至少支持5 MB的localStorage并且您的数据行看起来很小。 There is no fixed limit on Javascript variable size. Javascript变量大小没有固定限制。

Putting it in a hidden <div> will slow down initial page load. 将其放在隐藏的<div>会减慢初始页面加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM