-
Notifications
You must be signed in to change notification settings - Fork 164
/
Jokes.html
129 lines (117 loc) · 3.3 KB
/
Jokes.html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Jokes API</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background: cornflowerblue;
font-family: "Redressed",cursive;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
padding: 20px;
}
.container{
background-color: white;
border-radius: 10px;
box-shadow: 0 10px 20px 10px rgba(0, 0, 0, 0.1);
padding: 50px 20px;
text-align: center;
max-width: 100%;
width: 800px;
}
h3{
margin: 0;
color: rgb(148,141,141);
letter-spacing: 2px;
text-transform:capitalize;
}
.joke{
font-size: 30px;
letter-spacing: 1px;
line-height: 40px;
margin: 50px auto;
max-width: 600px;
}
.btn{
background-color: yellow;
color: white;
border: 0;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1), 0 6px 6x rgba(0, 0, 0, 0.1);
padding: 12px 30px;
font-size: 18px;
cursor: pointer;
outline: none;
font-family: "Mulish", sans-serif;
font-weight: 500;
}
.btn:hover{
background-color: rgb(250, 195, 16);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
</style>
<body>
<div class="container">
<h3>Best Jokes to make your day!</h3>
<div id="joke" class="joke">Awesome joke is loading.....</div>
<button id="jokebtn" class="btn">Next Joke</button>
</div>
</body>
<script>
// PROMISES
// data milega(fullfilled) => good || ya nahi milega(reject) => error throgh fetch
// we consume the promises na ki create(in 90 %)
// we use promises to escape from callback hell
// const jokes = document.querySelector('#joke');
// const jokeBtn = document.querySelector('#jokebtn');
// const generateJokes = () =>{
// const setHeader = {
// headers:{
// Accept : "application/json"
// }
// }
// fetch('https://icanhazdadjoke.com/',setHeader)
// .then((res) => res.json() ) // html to json
// .then((data)=>{
// jokes.innerHTML = data.joke;
// }).catch((error)=>{
// console.log(error);
// })
// }
// jokeBtn.addEventListener('click',generateJokes);
// generateJokes();
// Async await
const jokes = document.querySelector('#joke');
const jokeBtn = document.querySelector('#jokebtn');
const generateJokes = async() =>{
try{
const setHeader = {
headers:{
Accept : "application/json"
}
}
const res = await fetch('https://icanhazdadjoke.com/',setHeader);
const data = await res.json(); //html to json
jokes.innerHTML = data.joke ;
}catch(err){
console.log(`The error is ${err}`);
}
}
jokeBtn.addEventListener('click',generateJokes);
generateJokes();
</script>
</html>