-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (41 loc) · 1.49 KB
/
script.js
File metadata and controls
60 lines (41 loc) · 1.49 KB
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
//42nIb2L2iDo4pRmvfs2OFVFLPQoTJeNKJ2o0-C_su4o
const API_KEY = "42nIb2L2iDo4pRmvfs2OFVFLPQoTJeNKJ2o0-C_su4o";
const API = "https://api.unsplash.com/photos/random/?client_id=";
const img = document.getElementById("imageId");
const reload = document.querySelector("button");
const caption = document.getElementById("caption");
const loadImage = async () => {
try {
const data = await fetch(API + API_KEY);
const json = await data.json();
const image_url = json.urls.small;
img.src = image_url;
if(!image_url){
caption.innerText = "Wait!, Image not loaded yet";
} else {
caption.innerText = "";
}
caption.innerText = json.description;
} catch (error) {
console.log();("Error : " + error)
}
// let xhr = new XMLHttpRequest();
// xhr.open('GET', 'https://api.unsplash.com/photos/random/?client_id=42nIb2L2iDo4pRmvfs2OFVFLPQoTJeNKJ2o0-C_su4o');
// xhr.onload = function(){
// let imageData = JSON.parse(xhr.response);
// console.log(imageData);
// img.src = imageData.urls.small;
// caption.innerText = imageData.description;
// console.log(caption);
// console.log(img.src);
// };
// xhr.onerror = function() {
// console.log('Something went wrong...');
// };
// xhr.send();
}
if(!img.src){
caption.innerText = "Api call exceeded";
}
reload.addEventListener('click', loadImage)
loadImage();