【发布时间】:2017-07-18 09:11:31
【问题描述】:
我正在尝试将聊天消息保存在localStorage(或sessionStorage)并显示在网页上,我可以找到存储在Devtool -> application -> localStorage 中的键值对,每次用户发送消息时消息值都会更新并显示在网页上。但是,每次重新加载页面时内容都会消失。如何解决?
另外我遇到的是将消息保存到数组的 push() 将替换而不是增加值,不确定这两个问题是否相关。谢谢。
哈巴狗文件
#test2(style='height:200px; width:200px; border:1px solid #ccc')
js文件
$('#sendMessage').on('click', function() {
var msg = $('#message').val()
var messages = [];
console.log(typeof(messages)); //obj
messages.push(msg);
console.log(messages); //array value
if (localStorage) {
for (var i = 0; i < messages.length; i++) {
localStorage.setItem('message', msg);
}
localStorage.setItem('username', $('#username').val());
localStorage.setItem('date', currentTime());
var cUser = localStorage.getItem('username');
var cMsg = localStorage.getItem('message');
var cTime = localStorage.getItem('date');
} else {
console.log('localStorage is not supported.');
}
document.getElementById("test2").innerHTML += "<div>" + cUser + " : " + cMsg + "</div>";
// Clear the field
$('#message').val('');
}); // End click
重新加载页面丢失数据
// Send Message
$('#sendMessage').on('click', function() {
// get value
var username = $('#username').val();
var msg = $('#message').val();
var time = currentTime();
//check if localStorage works and create msgArray
if (!localStorage) {
console.log('localStorage is not supported.');
} else {
//check if there is existing message array in msgArray
var msgArray = localStorage.getItem('message');
//if there is NULL, setup msgArray = [], converts a JavaScript value to a JSON string
if (JSON.stringify(msgArray) == 'null') {
msgArray = [];
} else {
//else parses a JSON string, constructing the JavaScript value or object described by the string
msgArray = JSON.parse(msgArray);
}
}
//add new message object to msgArray
var newMsg = {
msg: msg,
username: username,
time: time
};
msgArray.push(newMsg);
// stringsfy the message and store it to localStorage
localStorage.setItem('message', JSON.stringify(msgArray));
// dispaly current messages
var cMsg = JSON.parse(localStorage.getItem('message'));
// console.log(cMsg); // should shows list of array with new added message objects
// for (var i = 0, max = cMsg.length; i < max; i++) {
//document.getElementById("test2").innerHTML += "<div>" + cMsg[i].username + " : " + cMsg[i].msg + " at " + cMsg[i].time + " </div>";
$('#test2').append('<div class="well well-sm">' + cMsg[cMsg.length - 1].username + ' : ' + cMsg[cMsg.length - 1].msg + ' <span class="pull-right"><small id="date"> at ' + cMsg[cMsg.length - 1].time + '</small></span></div>');
// load the bottom of message
var objDiv = document.getElementById("chatArea");
objDiv.scrollTop = objDiv.scrollHeight;
// Clear the field
$('#message').val('');
}); // End click
【问题讨论】:
-
messages有什么用= [];当您在 localStorage 中保存味精时
-
您正在覆盖 localStorage 中的消息,并且您没有从 localStorage 中检索它
-
这是一个实时聊天,如果我没弄错用户会提交消息,所以它需要遍历用户发送的所有消息并显示在客户端。
-
@ShubhamKhatri 你能指定哪个部分吗?
-
你正在覆盖本地存储中的消息
localStorage.setItem('message', msg);
标签: javascript jquery arrays html local-storage