The use of Localstorage

Source: Internet
Author: User


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

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.