Example Analysis of localStorage in JavaScript, h5localstorage instance

Source: Internet
Author: User

Example Analysis of localStorage in JavaScript, h5localstorage instance

This article describes how to store localStorage objects in JavaScript. We will share this with you for your reference. The details are as follows:

[Local storage limitations] The local storge restrictions in JavaScript are mentioned. In this example, a bool type data is stored in localStorage, but it is not stored as we expected.

When we store the Boolean, numeric, and string types, the localStorage object converts the stored data to the string literal by default.

LocalStorage [0] = false; // "false" localStorage [1] = 1200; // "1200" localStorage [2] = "wtf"; // "wtf"

There seems to be nothing above, but the boolean data is invalid. What if we change the stored data to another type? Such as arrays, literal objects, and objects.

Var obj = new Object (); obj. name = "obj"; obj. type = "obj"; localStorage [3] = ["160", "170", "180"]; // "160,170,180" localStorage [4] = {"id ": "0001", "name": "lee"}; // "[object Object]" localStorage [5] = obj; // "[object Object]"

The Object is converted to a type string. Therefore, JSON. stringify and JSON. parse must be used to store objects and other data. convert them into strings and then try to restore the data.

Related Article

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.