Spaces:
Running
Running
Update index.html
Browse files- index.html +42 -19
index.html
CHANGED
@@ -1,19 +1,42 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8" />
|
5 |
+
<title>Random Chuck Norris Jokes</title>
|
6 |
+
<style>
|
7 |
+
body{font-family:Arial,sans-serif;text-align:center}
|
8 |
+
h1{color:#ff6700;}
|
9 |
+
#jokeButton{padding:1rem 2rem;border-radius:.5rem;background-color:#ffcc00;cursor:pointer;transition:opacity.2s ease-in-out}
|
10 |
+
#jokeButton:hover{opacity:.9}
|
11 |
+
#jokeDisplay{margin-top:2rem;max-width:80%;margin-left:auto;margin-right:auto}
|
12 |
+
</style>
|
13 |
+
</head>
|
14 |
+
|
15 |
+
<body>
|
16 |
+
<h1>Welcome to Random Chuck Norris Jokes!</h1>
|
17 |
+
|
18 |
+
<button id="jokeButton">Fetch Joke</button>
|
19 |
+
<p id="jokeDisplay"></p>
|
20 |
+
|
21 |
+
<script>
|
22 |
+
// Add listener for button click
|
23 |
+
document.getElementById('jokeButton').addEventListener('click', function() {
|
24 |
+
// Fetch joke data from endpoint
|
25 |
+
fetch("https://api.chucknorris.io/jokes/random")
|
26 |
+
.then(function(response){
|
27 |
+
return response.json();
|
28 |
+
}).then(function(data){
|
29 |
+
|
30 |
+
var oldJokeNode = document.querySelector("#jokeDisplay");
|
31 |
+
|
32 |
+
if (oldJokeNode!== null && oldJokeNode.childNodes[0]!= undefined){
|
33 |
+
oldJokeNode.replaceChild(document.createTextNode(data.value), oldJokeNode.childNodes[0]);
|
34 |
+
} else {
|
35 |
+
// No child node exists yet - append text directly
|
36 |
+
oldJokeNode.appendChild(document.createTextNode(data.value));
|
37 |
+
}
|
38 |
+
});
|
39 |
+
});
|
40 |
+
</script>
|
41 |
+
</body>
|
42 |
+
</html>
|