Created
January 3, 2016 05:57
-
-
Save Borgaard/b70276015bc9550654c6 to your computer and use it in GitHub Desktop.
All the chats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<div id="chat-window"></div> | |
<div id="chat-form"> | |
<form id="formy"> | |
<textarea name="chat-entry" id="chat-entry" cols="30" rows="2"></textarea> | |
<input type="submit" value="ChatzOr"></input> | |
</form> | |
</div> | |
<script src="https://cdn.firebase.com/js/client/2.2.1/firebase.js"></script> | |
<script> | |
var firebase = new Firebase("https://sochatty.firebaseio.com/"); | |
firebase/*.limitToLast(100)*/.on('child_added', function (snapshot) { | |
var data = snapshot.val(); | |
console.log(data); | |
}); | |
var chatWindow = document.querySelector("#chat-window"); | |
firebase.once("value", function(snapshot){ | |
// var funnyExists = snapshot.exists(); | |
// console.log(snapshot.val()); | |
var snappy = snapshot.val(); | |
for (var key in snappy){ | |
var post = snappy[key]; | |
var li = chatWindow.appendChild(document.createElement("li")); | |
li.textContent = (new Date(post["time"])).toUTCString() + " | " + post["username"] + " says: " + post["messageBody"]; | |
} | |
}) | |
//on page load, check Local Storage to see if there's already a username | |
var username = localStorage.getItem("username"); | |
//if there is no username, ask the user and set value in local storage | |
if (username == null) { | |
username = prompt("What is your username?"); | |
localStorage.setItem("username", username); | |
} | |
var chatVal = document.querySelector("#chat-entry"); | |
var form = document.querySelectorAll("#formy")[0]; | |
form.addEventListener("submit", function(event){ | |
event.preventDefault() | |
// console.log(event.timeStamp); | |
console.log(arguments); | |
firebase.push({ | |
username: username, | |
messageBody: chatVal.value, | |
time: event.timeStamp | |
}) | |
var li = chatWindow.appendChild(document.createElement("li")); | |
li.textContent = (new Date(event.timeStamp)).toUTCString() + " | " + username + " says: " + chatVal.value; | |
chatVal.value = ""; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment