forked from Mahmoudgalalz/Learn-HTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (44 loc) · 1.14 KB
/
index.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
const input=document.getElementById("input")
const btn=document.getElementById('btn');
const text=document.getElementById('text')
const kindBtn=document.getElementById('kindBtn');
let kind="cat";
function ChangeTheHTTPAnimal(){
if(kindBtn.textContent!="Cats")
{
kindBtn.textContent="Cats"
kind="cat"
}
else{
kindBtn.textContent="Dog"
kind="dog"
}
}
function search(){
const code=input.value;
getData(code);
console.log(kind);
const img=`https://http.${kind}/${code}.jpg`
putData(img);
}
function getData(code){
fetch(`../public/Http-data/${code}.json`).then(response => {
return response.json();
}).then(data => {
textPut(data.Content)
}).catch(err => {
fetch(`../public/Http-data/default.json`).then(response => {
return response.json();
}).then(data => {
textPut(data.Content)
});
});
}
function putData(img){
document.getElementById("img").src=img;
}
function textPut(data){
text.innerHTML=data;
}
kindBtn.addEventListener('click',ChangeTheHTTPAnimal)
btn.addEventListener('click',search);