Localstorage implement _javascript tips for small notes

Source: Internet
Author: User

Preface: Today I wrote a little practice to consolidate the things I saw these days, and I used the conversion of localstorage storage and JSON string in this program.

Here is the specific implementation code:

(1) First to determine whether there is a user, thereby displaying the corresponding interface

function IsUser ()
{
  var storage = window.localstorage;
  if (storage.user!= undefined)
  {
    document.getElementById ("showmess"). Style.display = "block";
    document.getElementById ("Fillmess"). Style.display = "None";
  }
  else
  {
    document.getElementById ("showmess"). Style.display = "None";
    document.getElementById ("Fillmess"). Style.display = "block";
  }  
}

(2) Store user information

var pic; Global variables Store Picture info
/* Get picture from local computer */
 function add_files (files) {
        if (files.length) {
          var file = files[0];
          var reader = new FileReader ();
          Reader.onload = function (e) {
            document.getElementById ("Userpic"). src = e.target.result;
            pic = E.target.result;
          }
          Reader.readasdataurl (file);
        }
function saveusermess ()
{
  var name = document.getElementById ("username"). Value;
  if (name!= "")
  {
    var storage = window.localstorage;
    var jsonobj = {
                name:name,
                icon:pic
              }; 
      var user = json.stringify (jsonobj);
      Storage.setitem ("user", user);
  }
  else
  {
    alert ("nickname is not null"); 
  }


(3) The user enters the content of the sticky note and saves

function Save_diary ()
{
 var headle = document.getElementById ("Headle"). Value;
 var cont = document.getElementById ("Diary"). Value;
 if (Headle!= "" && cont!= "")
 {
  var storage = window.localstorage;
  if (storage.diary!= undefined)
  {
   var diary = Storage.getitem ("Diary");
   var jsonobj = Json.parse (diary);
   var Diaryitem = {
        Headle:headle,
        diary:cont,
        date:new date ()
       }; 
   Jsonobj.push (Diaryitem);
   var data = json.stringify (jsonobj);
   Storage.setitem ("Diary", data); 
  }

  else
  {
   var jsonobj = [{
        headle:headle,
        diary:cont,
        date:new date ()
       }]; 
   var diary = json.stringify (jsonobj);
   Storage.setitem ("Diary", Diary);
  }
 else
 {
  alert ("title and content not NULL"); 
 }


(4) Display the user sticky note information in the meeting

function Showdiary ()
{
  var storage = window.localstorage;
  var mess = Json.parse (storage.user);
  if (storage.diary!= undefined)
  {
    var json = Json.parse (storage.diary);
    var div_id = document.getElementById ("diarymess");
    for (var i = 0; i < json.length i++)
    {

      div_id.innerhtml+= ' <div><button class= ' btn btn-success ' ' + ' Type= "button" id= "Dropdownmenu" onclick= "show (This)" ><div class= "col-md-4" ><br><font style= "Text-align:center" > ' +mess.name+ ' </font>< /div><div class= "col-md-8" > 
 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.