1. In HTML5, local storage is a window property, including Localstorage and Sessionstorage, which is always local, and the latter is accompanied by a session, and the window disappears once it is closed. They use exactly the same
2. The way to store data is to add a property directly to Window.localstorage, localstorage the data is in the form of key-value pairs, you can use the GetItem () and SetItem () methods to set up and get data.
3. To store the data of the same object name multiple times in the localstorage, you need to get a property from Localstorage, if the value of the property is null, an array is created, and if present, the data is taken out using the Json.parse () The method is first converted to JSON, then the data is stored in, and then the array is taken out, and the Json.stringify () method is converted into a string form.
var student = { name:"Lily ", age : $var students = localstorage.students; if (! students) { = []; } Else { = json.parse (students); } Students.push (student); = json.stringify (players);
4.HTML5 Local storage can only store strings , and any format stored will be automatically converted to strings, so when reading, you need to do the type of conversion.
5. Why type conversion, localstorage itself will not be converted? Yes, but it is cast with tosting (), the converted is meaningless string, or is not what we want, so, in order to be able to manipulate the data, We need to use a special method to convert. Here's the difference between the two
var student = { name:' haha ', age : $ }; Console.log (Student.tostring ()); // [Object Object] Console.log (json.stringify (student)); // ' {' name ': ' Haha ', ' age ': 20} ' valid JSON string.
6.json.parse () resolves a * * valid String * * to a JSON object.
The use of Localstorage