Localhot Local Storage refresh local data still on the issue

Source: Internet
Author: User
Tags sessionstorage

The biggest difference between H5 's two storage technologies is the life cycle.

1. Localstorage is local storage, storage period is not limited;

2. Sessionstorage session storage, the page shutdown data will be lost.

How to use:

Localstorage.setitem ("Key", "value")//Storage

Localstorage.getitem (key)//value by key

Localstorage.valueof ()//Get all values

Localstorage.removeitem ("key")//delete a single value, note the quotation marks

Localstorage.clear ()//Delete all data

localstorage.length//number of data obtained

Localstorage.key (n)//key value for nth data

Note: Localstorage and sessionstorage Ibid, same method of use

a few general summary:

Localstorage.key = 1;//Set store, named Key, with a value of 1

Localstorage.removeitem ("key");//Remove store key, remember key plus quote

Here is a practical example of testing:

To implement the input text content stored locally in order to close the browser, and then reopen, the previous input is still in (common in the phone spike log in the field of leave and other fields entry).

First, get a text field <textarea></textarea> on the page, and here's jquery:?

1 2 3 4 5 6 7 if (!localstorage.getitem ("text"))//window object, the front window omits the Oh Localstorage.setitem ("text", "");  Here to judge, do a blank storage, or return NULL to show that the experience is not good, where the IF brace omitted localstorage.text = Localstorage.getitem ("text");  Value $ ("textarea"). HTML (localstorage.text); Displays $ ("textarea"). KeyUp (function () {//There are many, such as Blur, change, KeyDown, and a timer, which is used for multi-field storage Localstorage.setit  EM ("Text", $ (this). Val ()); Re-store});

The above can achieve a practical small function, the embodiment of H5 local storage is very useful, of course, there are many fields, provided with a JSON method to use OH. See below, download from network?

1 2

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.