Skip to content

Instantly share code, notes, and snippets.

@grifdail
Created July 21, 2013 12:30
Show Gist options
  • Save grifdail/6048422 to your computer and use it in GitHub Desktop.
Save grifdail/6048422 to your computer and use it in GitHub Desktop.
BugJS
window.addEventListener("load",function () {
var postComment = document.getElementById('postComment')
postComment.addEventListener("submit",function(e) {
e.preventDefault();
var xhr = new XMLHttpRequest(),
data = new FormData(postComment);
xhr.open("POST","/postCom");
data.append("withJSON","true")
xhr.addEventListener("load",function() {
document.getElementById("submit").disabled = false;
var result = JSON.parse(xhr.responseText);
if (result.error) {
drawError(result.error)
} else {
addComment(result.com)
}
});
xhr.send(data);
document.getElementById("submit").disabled = true;
var errordiv = document.getElementById("postError")
if (errordiv != null) {errordiv.parentNode.removeChild(errordiv);}
})
})
function addComment (comment) {
var article = document.createElement("article"),
header = document.createElement("header"),
circle = document.createElement("div"),
blogTitle = document.createElement("div"),
h6 = document.createElement("h6"),
time = document.createElement("time"),
p = document.createElement("p");
header.classList.add("comment");
circle.classList.add("circle");
circle.classList.add("color");
circle.classList.add("false");
p.classList.add("comText");
blogTitle.classList.add("blogTitle");
p.textContent = comment.Text;
h6.textContent = comment.Pseudo;
time.textContent = comment.frenchDate;
blogTitle.appendChild(h6);
blogTitle.appendChild(time);
header.appendChild(circle);
header.appendChild(blogTitle);
article.appendChild(header);
article.appendChild(p)
var section = document.getElementById('Comment');
if (section != null) {
section.insertBefore(article,section.children[1]);
} else {
section = document.createElement("section");
var headBar = document.createElement("h3")
section.classList.add("siteSection");
section.classList.add("turquoise");
headBar.classList.add("sectionTile");
headBar.classList.add("color");
headBar.innerText = "Commentaires";
console.log(headBar.textContent)
section.appendChild(headBar);
section.appendChild(article);
document.querySelector(".main").insertBefore(section,document.getElementById("post"))
}
}
function drawError (error) {
var errordiv = document.createElement("div")
errordiv.id = "postError"
for (var i = 0; i < error.length; i++) {
var p = document.createElement(p)
p.textContent = error[i]
errordiv.appendChild(p)
};
document.getElementById("postComment").appendChild(errordiv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment