HTML5 localstorage (Local storage)

Source: Internet
Author: User
Tags sessionstorage

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)

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.