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?