Localstorage a summary of problems with storing arrays or objects

Source: Internet
Author: User
Tags sessionstorage

When you Save the selected check box record (jump to the next page to return when you want the original selected) , the Localstorage storage data is used.

Because the check box has a bit more content, the object is stored in the Localstorage (because the check box may have multiple options, the length is not necessarily, and no two-dimensional array is used).

//The object that stores the check box is first declared CheckarrvarCheckarr={};//storing data in a CheckarrfunctionStorecheckarr (checkname) {//Each column check box is an array: checkname:[]checkarr[checkname]= []; $("Input[name= '" "+checkname+" ']:checked "). each (function(i) {checkarr[checkname][i]=$( This). Val (); });}//store data Checkarr to Localstorage when the button is clicked$ (". Btn-click"). Click (function(){    //"XXX" is the name of each item in the checkboxStorecheckarr ("xxx"); Storecheckarr ("XXX"); Storecheckarr ("XXX"); ......   /*This is to prevent multiple storage times, historical data has an impact on new data, so to remove*/Localstorage.removeitem ("Checkarr"); //json.stringify (Checkarr) is a must have, painful experience tell meLocalstorage.setitem ("Checkarr", Json.stringify (Checkarr));});

Read the data and restore the previously selected state

//Storeflag is returned when the page comes with an identity storeflag=1 stating that this is the returnif(storeflag==1){    /*the same time when reading also to transform, I was Leng didn't write this, has not been taken out data or extracted data is very strange*/Checkarr=json.parse (Localstorage.getitem ("Checkarr")); $.each (checkarr["XXX"],function(i,element) {$ ("Input[name= ' xxx '][value=" +element+ "]). attr (" Checked ",true); //the content here can be written according to your needs.            ......    });}/*I put this piece of code in $ (document). Ready, when you want the page to load, it can be displayed*/

You can only store strings in Localstorage on other blogs, so json.stringify () and Json.parse () are used to store and remove them.

If you're accessing a single data, it's not that troublesome.

var xxx= "xxx";localstorage.setitem ("xxx", xxx); XXX=localstorage.getitem ("xxx");

Summary: (from other blogs: http://blog.csdn.net/liuyan19891230/article/details/52638408)

Local storage, in addition to Localstorage, there are sessionstorage.
(1) Localstorage and sessionstorage are all objects used to store client temporary information.
(2) Localstorage and Sessionstorage can only store objects of type string
(3) Localstorage life cycle is permanent, unless the user manually clears the Localstorage information, otherwise this information will always exist.
(4) The Sessionstorage life cycle is the current window or tab, and once the window or tab is permanently closed, the data stored in the sessionstorage is emptied.
(5) Different browsers cannot share information in Localstorage or Sessionstorage. The same localstorage can be shared between different pages in the same browser (the page belongs to the same domain name and port), but sessionstorage information cannot be shared between pages or tabs .

Localstorage a summary of problems with storing arrays or objects

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.