JavaScript enables simple local data access and JSON data access

Source: Internet
Author: User

1. Determine if local storage is available:

if (window.localstorage) {   //Localstorge available   }else {  //Localstorge unavailable   }  

2. Store Data:

Gets the local storage object  var localstorage =window.localstorage;  Storage  Localstorage.setitem ("key", content);  

3. Remove the data

var myKey =localstorage.getitem ("key");  

4. Delete the specified data

Localstorage.removeitem ("key");  

5. Clear Local Data

Localstorage.clear ();  

6. Storing and reading JSON data

Define the JSON variable  var json ={username: "Yang Zhilai", Sex:1,age:25,birthday: "1990-08-21", Phonenumber:13812345678,disease: " Pulmonary nodules ", Insurance:1,avatar:" Images/male.png "};  JSON variable converted to JSON string  var jsonstr =json.stringify (JSON);  Store JSON string  window.localStorage.setItem ("Jsonkey", jsonstr);  Remove the JSON string  var jsonstr =window.localstorage.getitem ("Jsonkey");  Restore JSON object  var json =json.parse (JSONSTR);  Remove the username  var username =json.username inside the JSON;  

  

JavaScript enables simple local data access and JSON data access

Related Article

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.