Knowledge required for HTML5 mobile development-Local Storage (2)

Source: Internet
Author: User
Tags sessionstorage

Knowledge required for HTML5 mobile development-Local Storage (2)

After learning about some basic local storage usage and ideas, let's systematically introduce local storage.

Local Storage is divided into three categories: localStorage/sessionStorage/local database

LocalStorage and sessionStorage have the same usage, functions, and call methods. They only have different meanings. The data stored by localStorage is valid for a long time, and the information stored by sessionStorage is used for each session) the data will be destroyed when the page is closed (in other words, the data will be destroyed automatically after the page is closed ).

Because of their different features, the application scenarios are also very different. Generally, when we need to store some user configuration items and other data information that requires long-term storage, we need to use localStorgae for storage, taking advantage of its long validity period. Correspondingly, when we need to implement session-based functions like shopping cart, we need to use sessionStorage.

Because localStorage and sessionStorage have the same usage, we use localStorage as an example to introduce the methods of both.

1. Set setItem

Use localStorage. setItem ("key", "value") to pass the value to the key. (SessionStorage. setItem is used in the same way. We will not describe it one by one here)

2. Get Data getItem

The usage is localStorage. getItem ("key"). You only need to enter the corresponding key value to retrieve the corresponding value.

3. delete a specific data removeItem

Use localStorage. removeItem (key) to delete data corresponding to the key.

4. clear all data clear

LocalStorage. clear () indicates clearing all data in the storage system.

These are some of the most basic sessionStorage/localStorage usage.

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.