forked from HuXn-WebDev/HTML-CSS-JavaScript-100-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
25 lines (20 loc) · 656 Bytes
/
app.js
File metadata and controls
25 lines (20 loc) · 656 Bytes
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
const displayJoke = document.getElementById("display-joke");
const button = document.getElementById("getJoke");
button.addEventListener("click", getRandomJoke);
function getRandomJoke() {
const ajax = new XMLHttpRequest();
const url = "https://api.chucknorris.io/jokes/random";
ajax.open("GET", url, true);
ajax.onreadystatechange = () => {
if (ajax.status === 200 && ajax.readyState === 4) {
let data = JSON.parse(ajax.responseText);
displayJoke.innerHTML = `${data.value}`;
} else {
ajax.onerror = onerror();
}
};
ajax.send();
}
function onerror() {
displayJoke.textContent = `Something Went Wrong :(`;
}