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.