最終效果javascript
// 從window對象中獲取localStorage
window.localStorage複製代碼
window.localStorage.length複製代碼
window.localStorage.setItem("userName", "昭昭")複製代碼
window.localStorage.setItem("userName", "zhaoolee")複製代碼
window.localStorage.getItem("userName")複製代碼
window.localStorage.removeItem("userName")複製代碼
window.localStorage.clear()複製代碼
var storageLength = window.localStorage.length;
for(var i = 0; i < storageLength; i++) {
console.log(window.localStorage.key(i));
}複製代碼
function onLocalStorageChange(event) {
console.log(event.key);
}
window.addEventListener('storage', onLocalStorageChange);複製代碼
增刪改查
事件,都會觸發函數 onLocalStorageChange
增刪改查
, 只有在其它頁面(好比B頁面)才能觸發 onLocalStorageChange
實現的功能css
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet">
<title>開始聊天</title>
</head>
<body>
<div style="margin: 30px;">
<div class="input-group" style="width: 500px">
<label style="height: 40px; line-height: 40px" for="user-name">用戶名: </label><input type="text"
class="form-control" placeholder='請輸入你的名字( 默認爲「匿名用戶」 )' id="user-name" onblur="ChangeUserName(this)">
</div>
<!-- 存放 -->
<div id="chat-content" style="border: 1px solid #413F43; height: 300px; overflow: hidden; width: 600px;margin: 10px 0;padding: 10px;">
</div>
<div class="input-group" style="width: 600px">
<input type="text" class="form-control" placeholder="請輸入須要發送的內容" id="my-input">
<span class="input-group-btn">
<button class="btn btn-primary" type="button" style="margin-left: 10px" id="btn"> 發 送 </button>
</span>
</div>
</div>
<script>
//發送消息
var btn = document.getElementById("btn");
var myInput = document.getElementById("my-input");
var userName = document.getElementById("user-name").value || "匿名用戶";
var chatContent = document.getElementById("chat-content");
// 獲取當前的年月日,時分秒
function getDateTime() {
var dt = new Date();
var Year = dt.getFullYear();
var Month = dt.getMonth() + 1;
var Day = dt.getDate();
var Hours = dt.getHours();
var Minutes = dt.getMinutes();
var Second = dt.getSeconds();
return (Year + "-" + Month + "-" + Day + " " + Hours + ":" + Minutes + ":" + Second);
}
// 發送後存放到session
btn.addEventListener("click", function () {
if (myInput.value != "") {
var dateTime = getDateTime();
var userNameAndInputvalue = userName + ":" + myInput.value;
// 將時間用戶名和聊天內容寫入local storage
window.localStorage.setItem(dateTime, userNameAndInputvalue)
// 將時間用戶名和聊天內容添加到頁面中
chatContent.innerHTML = chatContent.innerHTML + "<div style='text-align: right'; border-bottom: 1px solid #413F43>" + dateTime+ "<br>" + userNameAndInputvalue +
"</div>"
myInput.value = "";
}
})
// 監聽session變化時, 更新聊天信息
window.addEventListener('storage', function (e) {
if((e["key"] != null)&&(e["newValue"] != null)){
var userNameAndInputvalue = "<div>" + e["key"] + "<br>" + e["newValue"] + "</div>";
chatContent.innerHTML = chatContent.innerHTML + "<div style='text-align: left'>" + userNameAndInputvalue + "</div>"
}
})
// 綁定回車
document.onkeydown = function (e) {
if (e.keyCode === 13) {
btn.click();
}
}
// 從新輸入用戶名
function ChangeUserName() {
userName = document.getElementById("user-name").value || "匿名用戶";
console.log("當前的用戶名爲:", userName);
}
</script>
</body>
</html>複製代碼
運行上面的聊天源碼, 須要開啓http服務, 能夠參考 www.jianshu.com/p/f60758991…html
博主前幾天寫網站,服務端向瀏覽器下發cookies, 因爲網絡環境不太好, cookies下發可能會失敗, 博主最後用Local Storage代替了cookies, 仍是蠻好用的, 哈哈, 仍是要主動擁抱新技術!java