Methods for storing array of objects in Localstorage and reading _javascript tips

Source: Internet
Author: User
Tags sessionstorage

frequent AJAX requests cause page responses to slow down. then consider storing the data in window.storage so that only one Ajax request is required, not frequent requests.

Since only strings can be stored in localstorage, we need to use the json.stringify () and Json.parse ();

$.ajax ({
type: "Get",
async: "True",
URL: "",
data: {},
DataType: "Jsonp",
success:function (data) {
if (data instanceof array) {
Stroage.push (json.stringify (data[i));//storage is an externally-defined array storage = []
}
},
error:function () {
}
});

Now we store data in storage, when we need to use:

Fucntion GetData () {
window.localStorage.job = json.stringify (storage);//convert Storage to string store
var job = Json.parse (window.localStorage.job);
for (var i = 0; i < job.length i++) {
Job[i] = Json.parse (job[i));
In this case, the job is storing an array of objects
}

Local storage, in addition to Localstorage, there are sessionstorage.

(1) Localstorage and Sessionstorage are objects that are used to store temporary information for the client.

(2) Localstorage and Sessionstorage can only store objects of string type

(3) The Localstorage lifecycle 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 through 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 (pages belong to the same domain name and port), but sessionstorage information cannot be shared between different pages or tabs.

The above is a small set to introduce the Localstorage in the storage object array and read the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.