-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (45 loc) · 1.55 KB
/
Copy pathapp.js
File metadata and controls
49 lines (45 loc) · 1.55 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
// function getUser() {
// fetch("https://api.chucknorris.io/jokes/random")
// .then(res => res.json())
// .then(data => {
// // data.value contains the joke
// document.getElementById("jock").innerText = data.value;
// })
// .catch(error => {
// console.error("Error fetching data:", error);
// });
// }
function getUser() {
document.getElementById("loader").style.display = "block";
document.getElementById("jock").innerText = "";
fetch("https://api.chucknorris.io/jokes/random")
.then((res) => res.json())
.then((data) => {
document.getElementById("loader").style.display = "none";
document.getElementById("jock").innerText = data.value;
document.getElementById("jokeIcon").src =
data.icon_url ||
"https://assets.chucknorris.host/img/avatar/chuck-norris.png";
document.getElementById(
"tweetBtn"
).href = `https://twitter.com/intent/tweet?text=${encodeURIComponent(
data.value
)}`;
})
.catch((error) => {
document.getElementById("loader").style.display = "none";
console.error("Error fetching data:", error);
document.getElementById("jock").innerText =
"⚠️ Failed to load joke. Try again!";
});
}
// Copy joke to clipboard
function copyJoke() {
const joke = document.getElementById("jock").innerText;
if (joke) {
navigator.clipboard.writeText(joke);
alert("✅ Joke copied to clipboard!");
} else {
alert("⚠️ No joke to copy yet!");
}
}