-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (62 loc) · 3.01 KB
/
script.js
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
const GnewsKey = 'e59508cf11c8163ca125b013c491abba';
//const newsKey = '1585ef9523994d5ebf088e18989877a9';
//fetch('https://gnews.io/api/v4/{endpoint}?token=e59508cf11c8163ca125b013c491abba');
let url = 'https://gnews.io/api/v4/top-headlines?max=5&topic=business&lang=en&token=' + GnewsKey;
//let url = 'https://newsapi.org/v2/top-headlines?country=us&apiKey=' + newsKey;
fetch(url)
.then(function(response){
return response.json();
}).then(function(json){
console.log(json);
let results = '';
results += "<h2>Breaking News Today</h2>";
for (let i = 0; i < json.articles.length; i++){
results += "<div class=article>"
results += "<a href='" + json.articles[i].url + "' target='_blank'><h6>" + json.articles[i].title;
results += "</h6><img src='" + json.articles[i].image +"' class = 'img-fluid'></a>"
results += "</div>"
}
document.getElementById('newPage').innerHTML = results;
})
document.getElementById('sumbit1').addEventListener('click', function(event){
event.preventDefault();
const value = document.getElementById('search1').value;
let url2 = 'https://gnews.io/api/v4/search?q=' + value + '&max=5&lang=en&token=' + GnewsKey;
console.log(url2);
fetch(url2)
.then(function(response){
return response.json();
}).then(function(json){
console.log(json);
let results = '';
results += "<h2>News about " + value + "</h2>";
for (let i = 0; i < json.articles.length; i++){
results += "<div class=article>"
results += "<a href='" + json.articles[i].url + "' target='_blank'><h6>" + json.articles[i].title;
results += "</h6><img src='" + json.articles[i].image +"' class = 'img-fluid'></a>"
results += "</div>"
}
document.getElementById('newPage').innerHTML = results;
})
});
document.getElementById('submit2').addEventListener('click', function(event){
event.preventDefault();
const value = document.getElementById('search2').value;
let url2 = 'https://gnews.io/api/v4/search?q=' + value + '&max=5&lang=en&token=' + GnewsKey;
console.log(url2);
fetch(url2)
.then(function(response){
return response.json();
}).then(function(json){
console.log(json);
let results = '';
results += "<h2>News about " + value + "</h2>";
for (let i = 0; i < json.articles.length; i++){
results += "<div class=article>"
results += "<a href='" + json.articles[i].url + "' target='_blank'><h6>" + json.articles[i].title;
results += "</h6><img src='" + json.articles[i].image +"' class = 'img-fluid'></a>"
results += "</div>"
}
document.getElementById('newPage').innerHTML = results;
})
});