-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
55 lines (47 loc) · 1.27 KB
/
main.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
function process_click(d) {
// get keyword value from form
var string = d.keyword.value;
// api url
giphyApiUrl = "http://api.giphy.com/v1/gifs/search?apikey=pLfr53QOcegFLeFIEvQCi15jaChIQuly&q="+string;
// let promise = fetch(giphyApiUrl);
// promise.then(getData);
// promise.catch(getErr);
// function getData(data) {
// console.log(data);
// }
// function getErr(err) {
// console.log(err);
// }
// above code simplified,,,,,getting data from api using promises
function ini_main() {
fetch(giphyApiUrl)
.then(res => {
return res.json()
})
.then(json => {
createImg(json.data);
})
.catch(err => console.log(err));
}
// print keyword in html
function createPara(word) {
var x = document.createElement('h1');
var t = document.createTextNode(word);
x.appendChild(t);
x.style.textTransform = "capitalize"
document.body.appendChild(x);
}
// create images from received urls
function createImg(urls) {
for (var i=0;i<urls.length;i++) {
var x = document.createElement('IMG');
x.setAttribute("src", urls[i].images['fixed_height_small'].url);
x.setAttribute("width", "304");
x.setAttribute("height", "228");
// x.setAttribute("alt", string);
document.body.appendChild(x);
}
}
createPara(string);
ini_main();
}