Detailed description of Html5 web local storage instances and html5web instances
Web Storage is a very important feature introduced by HTML5. It can store data locally on the client, similar to the cookie of HTML4. However, it has more powerful functions than cookies, the cookie size is limited to 4 kb, and the Web Storage officially recommends 5 MB for each website.
There are two types of Web Storage:
SessionStorage
LocalStorage
It can be clearly indicated from the literal meaning that sessionStorage saves the data in the session, and the browser closes, while localStorage keeps saving the data locally on the client;
The APIs available for both sessionStorage and localStorage are the same. Common APIs are as follows (using localStorage as an example ):
Save data: localStorage. setItem (key, value); read data: localStorage. getItem (key); delete a single data: localStorage. removeItem (key); Delete all data: localStorage. clear (); obtain the key of an index: localStorage. key (index );
As shown above, both the key and value must be strings. In other words, the web Storage API can only operate strings.
Next, we will develop a simple address book applet through Web Storage to demonstrate how to use related APIs. We will implement the following functions:
Enter the contact name and mobile phone number fields, and store the mobile phone number as the key to localStorage. Find the host based on the mobile phone number and list all the currently saved contact information;
First, write a simple html code.
Copy XML/HTML Code to clipboard
- <! DOCTYPEHTML>
- <Html>
- <Head>
- <Metacharsetmetacharset = "UTF-8"/>
- <Title> WebStorage for HTML5 local storage </title>
- </Head>
- <Body>
- <Divstyledivstyle = "border: 2 pxdashed # ccc; width: 320px; text-align: center;">
- <Labelforlabelfor = "user_name"> name: </label>
- <Inputtypeinputtype = "text" id = "user_name" name = "user_name" class = "text"/>
- <Br/>
- <Labelforlabelfor = "mobilephone"> mobile phone: </label>
- <Inputtypeinputtype = "text" id = "mobilephone" name = "mobilephone"/>
- <Br/>
- <Inputtypeinputtype = "button" onclick = "save ()" value = "add record"/>
- <Hr/>
- <Labelforlabelfor = "search_phone"> enter the mobile phone number: </label>
- <Inputtypeinputtype = "text" id = "search_phone" name = "search_phone"/>
- <Inputtypeinputtype = "button" onclick = "find ()" value = ""/>
- <Pidpid = "find_result"> <br/> </p>
- </Div>
- <Br/>
- <Dividdivid = "list">
- </Div>
- </Body>
- </Html>
After the page is written, the display effect is similar:
To save contacts, simply implement the following JS method:
Copy XML/HTML Code to clipboard
- Functionsave (){
- Varmobilephone = document. getElementById ("mobilephone"). value;
- Varuser_name = document. getElementById ("user_name"). value;
- LocalStorage. setItem (mobilephone, user_name );
- } // Used to save data
To find the host, implement the following JS method:
Copy XML/HTML Code to clipboard
- // Search for Data
- Functionfind (){
- Varsearch_phone = document. getElementById ("search_phone"). value;
- Varname = localStorage. getItem (search_phone );
- Varfind_result = document. getElementById ("find_result ");
- The host for find_result.innerHTML = search_phone + "is:" + name;
- }
To display all the saved contact information, use the localStorage. key (index) method as follows:
Copy XML/HTML Code to clipboard
- // Extract all objects stored in localStorage and display them on the Interface
- FunctionloadAll (){
- Varlist = document. getElementById ("list ");
- If (localStorage. length> 0 ){
- Varresult = "<tableborder = '1'> ";
- Result + = "<tr> <td> name </td> <td> mobile phone number </td> </tr> ";
- For (vari = 0; I <localStorage. length; I ++ ){
- Varmobilephone = localStorage. key (I );
- Varname = localStorage. getItem (mobilephone );
- Result + = "<tr> <td>" + name + "</td> <td>" + mobilephone + "</td> </tr> ";
- }
- Result + = "</table> ";
- List. innerHTML = result;
- } Else {
- List. innerHTML = "the current data is empty. Please join the contact ";
- }
- }
The effect is as follows:
Q: In the above demo, there are only two fields, name and mobile phone number. If you want to store more contact information, such as the company name and home address, how can this problem be achieved? Isn't Web Storage only capable of processing strings? In this case, you can use the stringify () method of JSON to convert complex objects into strings and store them in Web Storage. when reading from Web Storage, you can use the JSON parse () the method is then converted to a JSON object;
The following is a simple demonstration of adding a contact with the company property to save the JS Code:
Copy XML/HTML Code to clipboard
- // Save data
- Functionsave (){
- Varcontact = newObject;
- Contact. user_name = document. getElementById ("user_name"). value;
- Contact. mobilephone = document. getElementById ("mobilephone"). value;
- Contact. company = document. getElementById ("company"). value;
- Varstr = JSON. stringify (contact );
- LocalStorage. setItem (contact. mobilephone, str );
- LoadAll ();
- }
- // Extract all objects stored in localStorage and display them on the Interface
- FunctionloadAll (){
- Varlist = document. getElementById ("list ");
- If (localStorage. length> 0 ){
- Varresult = "<tableborder = '1'> ";
- Result + = "<tr> <td> name </td> <td> mobile phone </td> <td> company </td> </tr> ";
- For (vari = 0; I <localStorage. length; I ++ ){
- Varmobilephone = localStorage. key (I );
- Varstr = localStorage. getItem (mobilephone );
- Varcontact = JSON. parse (str );
- Result + = "<tr> <td>" + contact. user_name + "</td> <td>" + contact. mobilephone + "</td> <td>" + contact. company + "</td> </tr> ";
- }
- Result + = "</table> ";
- List. innerHTML = result;
- } Else {
- List. innerHTML = "the current data is empty. Please join the contact ";
- }
- }
The effect is as follows:
The above is a detailed description of the Html5 web local storage instance introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!