-
Notifications
You must be signed in to change notification settings - Fork 0
/
top50.js
66 lines (62 loc) · 2.27 KB
/
top50.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var xhr = new XMLHttpRequest();
var data;
xhr.onreadystatechange = function(){
//readyState 0-unsent 1-open has been called 2-send has been called 3-loading 4-done
//status UNSENT: 0 OPENED: 0 LOADING: 200 DONE: 200
if(this.readyState == 4 && this.status == 200){
//responseText returns the text received from a server following a request being sent.
//console.log(this.responseText);
//display(this.responseText);
data = JSON.parse(this.response);
data.top.forEach(element => {
console.log(element.title);
});
createList(data);
}
}
xhr.open("GET", "https://api.jikan.moe/v3/top/anime", true);
xhr.send();
/*
<div class="card">
<img src="img_avatar.png" alt="Avatar" style="width:100%">
<div class="container">
<h4><b>John Doe</b></h4>
<p>Architect & Engineer</p>
</div>
</div>
*/
function createList(data){
var contentBox = document.getElementById("topContent");
data.top.forEach(element =>{
let card = document.createElement("div");
card.setAttribute("class", "card");
card.onclick = function(){
document.location.href = element.url;
};
let cardBody = document.createElement("div");
cardBody.setAttribute("class", "container")
cardBody.setAttribute("id", "cardBody");
let image = document.createElement("img");
image.src = element.image_url;
image.width = 150;
image.height = 200;
let textBox = document.createElement("div");
textBox.setAttribute('style', 'white-space: pre;');
textBox.innerHTML = "Rank: ".bold() + element.rank +'\n'
+ "Score: ".bold() + element.score +'\n'
+ "Episodes: ".bold() + element.episodes;
cardBody.appendChild(image);
cardBody.appendChild(textBox);
let footer = document.createElement("div");
let title = document.createElement("h4");
title.innerHTML = element.title;
let detail = document.createElement("p");
detail.innerHTML = element.type;
footer.setAttribute("class", "container");
footer.appendChild(title);
footer.appendChild(detail);
card.appendChild(cardBody);
card.appendChild(footer);
contentBox.appendChild(card);
});
}