One 、 How to get localStorage Remaining capacity of

stay H5 Today's popularity ,localStorage( The local store ) It's no stranger to every former siege division . meanwhile localStorage It also brings us great convenience , Not used cookies A small family ,localStorage The amount of storage is still very objective .

Because of the differences in browsers , That leads us to need to localStorage Make a partial trade-off between the contents of . And according to the current market browser on loaclStorage From the compatibility of , The best size is 2.5M about . How to get localStorage Capacity size of , I've probably been in contact with localStorage It's a problem we all meet .

localStorage  Stored in is a string , According to this condition , We can do this by taking out all the localStorage The content of , And the length is the size . The specific code is as follows :

 1 (function(){
2 if(!window.localStorage) {
3 console.log(' Browser does not support localStorage');
4 }
5 var size = 0;
6 for(item in window.localStorage) {
7 if(window.localStorage.hasOwnProperty(item)) {
8 size += window.localStorage.getItem(item).length;
9 }
10 }
11 console.log(' At present localStorage The remaining capacity is ' + (size / 1024).toFixed(2) + 'KB');
12 })()

Two 、 How to get localStorage The maximum capacity

Through the above analysis , In fact, the idea is basically the same , It's all determined by the length of the characters .

 1 (function() {
2 if(!window.localStorage) {
3 console.log(' The current browser does not support localStorage!')
4 } var test = '0123456789';
5 var add = function(num) {
6 num += num;
7 if(num.length == 10240) {
8 test = num;
9 return;
10 }
11 add(num);
12 }
13 add(test);
14 var sum = test;
15 var show = setInterval(function(){
16 sum += test;
17 try {
18 window.localStorage.removeItem('test');
19 window.localStorage.setItem('test', sum);
20 console.log(sum.length / 1024 + 'KB');
21 } catch(e) {
22 console.log(sum.length / 1024 + 'KB Exceed the maximum limit ');
23 clearInterval(show);
24 }
25 }, 0.1)
26 })()

I wrote a Check the browser localStorage Maximum capacity of The page of , Though sparrows are small , But it has all five internal organs :lol:.

How to get a browser localStorage More articles about the remaining capacity of

  1. Check the page for localstorage Residual capacity

    First of all JSON.stringify(localStorage).length Get your current page localstorage Number of characters used current_num. Then you go to http://arty.nam ...

  2. Check the source code for sd Code for the remaining capacity of the card

    download android Source code , find app Under the Settings Application source code , Import Settings project (android Project source code ) lookup “ Available space ” obtain <string name="memory_av ...

  3. Using the browser LocalStorage Cache pictures , Video file

    Article path :https://hacks.mozilla.org/2012/02/saving-images-and-files-in-localstorage/

  4. 【ASM】 see ASM The remaining capacity and total capacity of the disk group

    col total_size for a10; col free_size for a20; select name,total_mb/1024 || 'G' as total_size , free ...

  5. localStorage and sessionStorage The difference between Firefox and other advanced browsers

    Example : <script>window.onload=function(){ var oSetData = document.getElementById('setData'); var o ...

  6. localStorage What to do when it's full ?

    Let's start with a few interview questions 1.a.meituan.com and b.meituan.com These two domains can share the same localStorage Do you ? 2. stay webview Open a page in :i.meituan.co ...

  7. Browser local storage (browser-storage,HTML5-localStorage &gt; IE-UserData &gt; Cookie)

    https://www.baidufe.com/component/browser-storage/index.html BrowserStorage It's a solution for browser local storage , The storage priorities are : ...

  8. [ Open source ]jquery-ajax-cache: Quickly optimize the page ajax request , Use localStorage Cache request

    project :jquery-ajax-cache Address :https://github.com/WQTeam/jquery-ajax-cache     Recently, local caching has been used in projects localStorage Do data ...

  9. [JavaScript] Cookie,localStorage,sessionStorage summary

    Cookie Cookie It's some data , Stored in a text file on your computer , When web The server sends... To the browser web When the page is , After the connection is closed , The server will not record the user's information .Cookie The function of is to store web page ...

Random recommendation

  1. js Block submission of forms (post)

    On the registration page , We often use page validation , Verify that the data does not meet the requirements to prevent submission to the server , as follows <script type="text/javascript"> function C ...

  2. linux Lower installation jdk+tomcat+eclipse+mysql

    My environment : The main engine is win7 Of , Virtual machine is VWare  Workstation 6.0   ,linux System is Red Hat Enterprise  Linux 5 64 position Software versions :jdk yes jdk-6u ...

  3. redis2.8-- memory management

    To make a long story short ,redis Memory management is mainly controlled by the operating system , Supplemented by simple packaging , Achieve simple and slightly improved performance . Memory block , Mark this block size As shown in the figure above , When calling zmalloc/zmalloc when , Input parameters ...

  4. hdu 4473 Exam mathematics

    Ideas : Turn the condition into satisfaction abc<=n Of abc Number of . 1.3 When the numbers are equal , by A; 2. Yes 2 When the numbers are equal , by B; 3. When they are not equal , by C. The result is A+3*B+6*C. The code is as follows : #includ ...

  5. bzoj1040

    On the ring dp The importance of ! In fact, this ring is relatively simple , Just a little analysis shows that , This is a simple ring , And each link block contains only one . I think the key to dealing with the ring is , It's about finding loops and lines ( Tree form ) What's the difference? . If we disconnect from somewhere, do dp Of ...

  6. Android Acquisition of difficult and miscellaneous diseases listView Item The value of the component above

    In fact, the idea is again findById once Get the value of the component . For example, the application scenario is Long click event Get the directory name @Override public boolean onItemLongClick(Adapter ...

  7. TagBuilder The performance is so low ?

    This article is from :http://www.cnblogs.com/zhuisha/archive/2010/03/12/1684022.html Need to pass through ASP.NET MVC Generate a list ,MVC It's very red inside ...

  8. UVa11882,Biggest Number

    Search for + prune Such a water problem , All morning The mistake is bfs And dfs I shared a vis Array , Lead to bfs Return to dfs What should be accessible is not accessible How do you want to prune , Thought of a few pruning methods , It also proves that , And overthrow , Think again , Prove again ...

  9. Talk about JAVA Decompilation of programs

      Now JAVA Language is in full swing all over the world , It is widely used in INTERNET The database of . multimedia .CGI. And the production of dynamic web pages .1999 In the United States in JAVA For the first time, the demand for programmers exceeds C++! Recent analysis of some ...

  10. distinguish getchar(),getch(),getche() Three functions :

    distinguish getchar(),getch(),getche() Three functions : The first line is manually entered , The second line is printf Output . getch() and getche() These two functions should include conio.h The header file : ge ...