Original address: http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/html5-localstorage/
HTML5 provides two new ways for clients to store data: Localstorage and Sessionstorage. The localstorage method is long-term, with no time limit for stored data. Data is still available after the second, second, or next year. The sessionstorage method is a temporary save, for a session for data storage. When the user closes the browser window, the data is deleted. Here is the main explanation of the use of localstorage.
Localstorage official recommendation 5M storage space, it can only store data in string format, so it is best to convert the data into JSON format each time it is stored, and then convert it back when it is removed.
For example, I use ANGULARJS:
Localstorage.setitem ("Name", Angular.tojson (panda));
Localstorage mainly has the following methods:
SetItem ("Key", "value"): Stores a value whose name is key, and updates value if key exists
GetItem ("key"): Gets the value with the name key and returns NULL if key does not exist
RemoveItem ("key"): Delete the information named "Key" , the value corresponding to this key will be all deleted
Clear (): empties all information in localstorage
Key (): Index of key
A simple example:
Localstorage.setitem ("name", "Panda");//Set Name to Pandalocalstorage.setitem ("name", "Jane"),//To overwrite the previous value, Now the value of name corresponds to Janelocalstorage.getitem ("name");//Gets the value of name, i.e. Janelocalstorage.removeitem ("name");// Remove the value of name and name Localstorage.clear ();//clear Localstorage all data function Foreachkey (callback) {//Traverse display Localstorage key for (var i = 0; i < localstorage.length; i++) {Callback (Localstorage.key (i)); }}
HTML5 localstorage (Local storage)