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.