forked from Lab1not/coranos.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
85 lines (75 loc) · 2.11 KB
/
index.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<head>
<title>Banano</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body onload="onLoad();">
<div id="banano"></div>
<ol>
<li><img class="small_image" src="https://i.imgur.com/Remo7td.png" /></li>
</ol>
<table>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>Alpha</td>
</tr>
<tr>
<td>2</td>
<td>Beta</td>
</tr>
</table>
<script>
const addChildText = (parent,childText) => {
const node = document.createTextNode(childText);
parent.appendChild(node);
return node;
}
const addChildElement = (parent,childType,childText) => {
const child = document.createElement(childType);
parent.appendChild(child);
if(childText !== undefined) {
child.appendChild(document.createTextNode(childText));
}
return child;
}
const loadJson = () => {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
loadJsonCallback(this.response);
}
}
xhttp.responseType = 'json';
xhttp.open('GET', 'index.json', true);
xhttp.send();
}
const loadJsonCallback = (json) => {
const banano = document.getElementById('banano');
addChildText(banano, 'Coranos Bananos');
const table = addChildElement(banano,'table');
const tableHeaderRow = addChildElement(table,'tr');
if(json.length == 0) {
return;
}
for (const [key, value] of Object.entries(json[0])) {
addChildElement(tableHeaderRow,'th',key);
}
json.forEach((jsonElt,jsonEltIx) => {
const tableDataRow = addChildElement(table,'tr');
for (const [key, value] of Object.entries(jsonElt)) {
addChildElement(tableDataRow,'td',value);
}
});
}
const onLoad = () => {
loadJson();
}
</script>
</body>
</html>