Using HTML5 to develop Android (4)---HTML5 local storage web Storage

Source: Internet
Author: User
Tags sessionstorage

Web storage is a very important feature introduced by HTML5, which stores data locally on the client, similar to HTML4 cookies, but is much more powerful than cookies, and the cookie size is limited to 4kb,web Storage is officially recommended for each website 5MB.

The Web storage is divided into two types:

    • Sessionstorage
    • Localstorage

It is clear from the literal meaning that Sessionstorage saves the data in the session and the browser shuts down, while Localstorage keeps the data on the client side;

Whether it is sessionstorage, or localstorage, can use the same API, commonly used are the following (in the case of Localstorage):

    • Save data: Localstorage.setitem (Key,value);
    • Read data: Localstorage.getitem (key);
    • Delete individual data: Localstorage.removeitem (key);
    • Delete all data: Localstorage.clear ();
    • Get the Key:localStorage.key (index) of an index;

As above, both key and value must be strings, in other words, the Web Storage API can only manipulate strings.

Next, we develop a simple address book applet through the Web Storage to demonstrate the use of the relevant API, and we want to implement the following functions:

    1. Enter the contact person, the contact person has the name, the mobile phone number 2 fields, with the mobile phone number as the key deposit localstorage;
    2. Based on mobile phone number, find the main machine;
    3. Lists all contact information that is currently saved;

First, prepare a simple HTML page, as follows:

[HTML]View Plaincopy
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>HTML5 Local Storage Web Storage Chapter </title>
  6. </head>
  7. <body>
  8. <div style="border:2px dashed #ccc; width:320px;text-align:center;" >
  9. <label for="user_name"> Name:</label>
  10. <input type="text" id="user_name" name="user_name" class="text"/ >
  11. <br/>
  12. <label for="Mobilephone"> Mobile:</label>
  13. <input type="text" id="Mobilephone" name="mobilephone"/>
  14. <br/>
  15. <input type="button" onclick="Save ()" value="new record"/>
  16. <hr/>
  17. <label for="Search_phone"> enter phone number:</label>
  18. <input type="text" id="Search_phone" name="Search_phone"/>
  19. <input type="button" onclick="Find ()" value="Lookup machine master"/>
  20. <p id="Find_result"><br/></P>
  21. </div>
  22. <br/>
  23. <div id="list">
  24. </div>
  25. </body>
  26. </html>
The interface is displayed as follows:

To achieve a contact save, simply implement the following JS method:

[JavaScript]View Plaincopy
    1. Save data
    2. function Save () {
    3. var mobilephone = document.getElementById ("Mobilephone"). Value;
    4. var user_name = document.getElementById ("user_name"). Value;
    5. Localstorage.setitem (Mobilephone,user_name);
    6. }
To implement the lookup machine master, the following JS method is implemented:

[JavaScript]View Plaincopy
    1. Find data
    2. function Find () {
    3. var search_phone = document.getElementById ("Search_phone"). Value;
    4. var name = Localstorage.getitem (Search_phone);
    5. var find_result = document.getElementById ("Find_result");
    6. find_result.innerhtml = Search_phone + "of the Machine Master is:" + name;
    7. }

To show all of your saved contact information, you need to use the Localstorage.key (index) method, as follows:

[JavaScript]View Plaincopy
  1. Extracts all objects stored in the Localstorage and presents them to the interface
  2. function Loadall () {
  3. var list = document.getElementById ("list");
  4. if (localstorage.length>0) {
  5. var result = "<table border= ' 1 ' >";
  6. Result + = "<tr><td> name </td><td> mobile number </td></tr>";
  7. For (var i=0;i<localstorage.length;i++) {
  8. var mobilephone = Localstorage.key (i);
  9. var name = Localstorage.getitem (mobilephone);
  10. Result + = "<tr><td>" +name+"</td><td>" +mobilephone+"</td></tr>";
  11. }
  12. Result + = "</table>";
  13. list.innerhtml = result;
  14. }else{
  15. list.innerhtml = "The current data is empty, quickly start to join the contact bar";
  16. }
  17. }
The effect is as follows:


Question: As shown on the demo, there are only 2 fields, name and mobile phone number, if you want to deposit more rich contact information, such as company name, home address, etc., how to achieve it? Can't Web storage only handle strings? At this point, the JSON stringify () method can be used to convert complex objects into strings and into Web Storage, which can be converted to JSON objects through the parse () method of JSON when read from the Web Storage.

The following simple demo adds the company attribute to the contact person to save the JS code:

[JavaScript]View Plaincopy
  1. Save data
  2. function Save () {
  3. var contact = new Object;
  4. Contact.user_name = document.getElementById ("user_name"). Value;
  5. Contact.mobilephone = document.getElementById ("Mobilephone"). Value;
  6. Contact.company = document.getElementById ("Company"). Value;
  7. var str = json.stringify (contact);
  8. Localstorage.setitem (CONTACT.MOBILEPHONE,STR);
  9. Loadall ();
  10. }
  11. Extracts all objects stored in the Localstorage and presents them to the interface
  12. function Loadall () {
  13. var list = document.getElementById ("list");
  14. if (localstorage.length>0) {
  15. var result = "<table border= ' 1 ' >";
  16. Result + = "<tr><td> name </td><td> phone </td><td> company </td></tr>";
  17. For (var i=0;i<localstorage.length;i++) {
  18. var mobilephone = Localstorage.key (i);
  19. var str = localstorage.getitem (mobilephone);
  20. var contact = json.parse (str);
  21. Result + = "<tr><td>" +contact.user_name+"</td><td>" +contact.mobilephone+"<  /td><td> "+contact.company+" </td></tr> ";
  22. }
  23. Result + = "</table>";
  24. list.innerhtml = result;
  25. }else{
  26. list.innerhtml = "The current data is empty, quickly start to join the contact bar";
  27. }
  28. }
The effect is as follows:

Using HTML5 to develop Android (4)---HTML5 local storage web 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.