Using cookies to implement Localstorage functions

Source: Internet
Author: User

You need to take advantage of the HTML5 localstorage in your project. However, when using this attribute, it is found that the target cannot be reached. After continuous inspection and elimination, the final reason is found:

The browsers used in the project support Localstorage, but they are not available, and the reason is unknown (presumably the configuration of the browser's environment variables may need to be used directly, but I am powerless to do so).

Finally, through the Internet query, found that can use cookies to achieve the function of localstorage (of course, the data is stored for a limited time), the code is as follows:

<script type="Text/javascript">//Create LocalStorage1varLocalstorage1class =function () { This. options ={Expires: -* -*3600,    }} Localstorage1class.prototype= {    //initial manifestation. Add an Expiration timeinit:function () {varDate =NewDate (); Date.settime (Date.gettime ()+ -* -*3600);  This. SetItem ('Expires', date.togmtstring ()); },    //intrinsic function parameter description (key) Check if key existsfinditem:function (key) {var BOOL=document.cookie.indexOf (key); if(BOOL<0 ){            return true; }Else{            return false; }       },       //get element value get element value return NULL if not presentgetitem:function (key) {vari = This. FindItem (key); if(!i) {            varArray = Document.cookie.split (';')                        for(varj=0; j<array.length;j++){                varArraysplit =Array[j]; if(Arraysplit.indexof (key) >-1){                     varGetValue = Array[j].split ('='); //Remove getvalue[0] trim to delete both spacesgetvalue[0] = getvalue[0].replace (/^\s\s*/,"'). Replace (/\s\s*$/,"')                    if(getvalue[0]==key) {                    returngetvalue[1]; }Else{                    return 'NULL'; }                }            }        }       },       //re-setting elementssetitem:function (key,value) {vari = This. FindItem (key) Document.cookie=key+'='+value; },       //Clear All cookie parametersremove:function () {varArray = Document.cookie.split (';')                        for(varCL =0; cl<array.length;cl++){            varDate =NewDate (); Date.settime (Date.gettime ()- -); Document.cookie=ARRAY[CL] +"=a; expires="+date.togmtstring (); }       }}              varLocalStorage1 =NewLocalstorage1class (); Localstorage1.init ();</script>

Reprint: http://blog.csdn.net/xiaosong521/article/details/7733424

Using cookies to implement Localstorage functions

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.