-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (46 loc) · 2.32 KB
/
script.js
File metadata and controls
67 lines (46 loc) · 2.32 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
61
62
63
64
65
66
67
//method One using preset quotes
// const quotes = [
// "The only way to do great work is to love what you do. — Steve Jobs",
// "In three words I can sum up everything I've learned about life: it goes on. — Robert Frost",
// "Life is 10% what happens to us and 90% how we react to it. — Charles R. Swindoll",
// "The best way to predict the future is to create it. — Peter Drucker",
// "Don't count the days, make the days count. — Muhammad Ali",
// "The only limit to our realization of tomorrow will be our doubts of today. — Franklin D. Roosevelt",
// "Success is not final, failure is not fatal: It is the courage to continue that counts. — Winston Churchill",
// "The only thing necessary for the triumph of evil is for good men to do nothing. — Edmund Burke",
// "To be yourself in a world that is constantly trying to make you something else is the greatest accomplishment. — Ralph Waldo Emerson",
// "Life is really simple, but we insist on making it complicated. — Confucius",
// ];
// const clickBtn = document.querySelector(".btn");
// const insertHere = document.querySelector(".space");
// const heading = document.querySelector(".heading h3");
// function lookForQuote() {
// const randQuoteIdx = Math.floor(Math.random() * quotes.length);
// const randItem = quotes[randQuoteIdx];
// heading.innerHTML = randItem;
// }
// clickBtn.addEventListener("click", lookForQuote);
//Method two using quote API
const clickBtn = document.querySelector(".btn");
const insertHere = document.querySelector("#quote");
const author = document.querySelector("#author");
function generateQuote() {
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.quotable.io/random");
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
const data = JSON.parse(this.responseText);
insertHere.innerHTML = data.content;
author.innerHTML = `Author - <strong>${data.author}</strong>`;
} else {
insertHere.innerHTML = "Error " + this.status;
author.innerHTML = "Not found :(";
}
}
};
xhr.send();
}
//Eventlisteners
clickBtn.addEventListener("click", generateQuote);
document.addEventListener("DOMContentLoaded", generateQuote);