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.