Localstorage and sessionstorage

Source: Internet
Author: User
Tags browser cache sessionstorage

Since HTML5 knowledge is involved, there must be some compatibility issues, and the browser support is involved here.

Browser support:

Internet Explorer 8 +, Firefox, opera, chrome, and Safari Support Web storage.

Note:Internet Explorer 7 and earlier ie versions do not support web storage.

Two methods are involved: localstorage and sessionstorage.

  (1) localstorage:

Localstorage (Local Storage) can store data for a long time without time restrictions. data can be used for one day, one year, two years, or even longer. In localstorage, browsers generally support 5 MB of size, which may vary with browsers.

Advantages:

Although the storage size is limited, it is much larger than the cookie.

Disadvantages:

Need to be manually deleted, otherwise it will exist for a long time
Different browser sizes and different versions are supported
Localstorage only supports string storage, and JSON objects need to be converted.

Common usage:

  • Save data: localstorage. setitem (Key, value );
  • Read data: localstorage. getitem (key );
  • Delete a single data: localstorage. removeitem (key );
  • Delete all data: localstorage. Clear ();
  • Obtain the key of an index: localstorage. Key (INDEX );

  Tip:Key-value pairs are usually stored as strings.

  (2) sessionstorage:

  Sessionstorage (Session storage) is only used before the browser is closed. It is allowed to be used when another page is created. After the browser is closed, the data disappears.

Features:

  • Same-source policy restrictions. If you want to operate on the same sessionstorage between different pages, these pages must be under the same protocol, the same host name, and the same port.
  • Stored locally only. The seesionstorage data will not be sent to the server along with the HTTP request. It will only take effect locally and clear the data after the tab is closed.
  • Storage Method. Seesionstorage uses key and value storage. The value must be of the string type. (If a non-string is input, it is also converted to a string during storage .).
  • Storage Limit: different browsers have different storage limits, but most browsers limit the Storage Limit to less than 5 MB.
   Same as localstorage
  
Before using web storage, check whether the browser supports localstorage and sessionstorage, for example:
If (typeof (storage )! = "Undefined") {// yes! Supports localstorage sessionstorage objects! // Some code...} else {// sorry! Web storage is not supported .}

  

Here we use localstorage as an example (the example is directly copied from elsewhere, but this is not the focus ):

<HTML lang = "en"> Document. getelementbyid ("result"). innerhtml = "you have already clicked the button" + localstorage. clickcount + "times";} else {
Document. getelementbyid ("result"). innerhtml = "sorry, your browser does not support web storage. ";}}</SCRIPT>

Here I use Chrome, Firefox, Safari, QQ, ie (I use ie11 on my computer), edge and other browsers for testing, we found that Chrome, Firefox, Safari, and QQ browsers can achieve the expected results, while IE and edge browsers cannot achieve the expected results. The reason why I thought the browser cache was still invalid after I knew it, I suddenly came up with the idea of whether IE browser supports localstorage. Here I will test it in a small demo: 

<script>    if(window.localStorage){        alert("support")    }else{        alert("not support")    }</script>        

  

Result in IE:

  

 

After checking the information, I found that IE8 and above all support localstorage. Then I went to the cainiao tutorial and copied an example to find that the problem was the same, however, I opened the link of the example in the cainiao tutorial in various browsers and found that it could run normally. What happened? after reading the information from multiple parties, I came to the conclusion that, Localstorage in IE does not support local files, that is, it must be supported on the server.To verify whether the conclusion is correct, I put the demo under the Tomcat server and open it in the IE browser (Note: In the IE browser, enter http: // localhost: 8080 (my port number is 8080). Otherwise, an error may occur. In other browsers, you only need to enter localhost: 8080.) The expected results can be achieved.
Therefore, the final conclusion is that localstorage in IE does not support local files and must be supported on the server. Similarly, sessionstorage in IE does not support local files, it also needs to be supported under the server

 

Localstorage and sessionstorage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.