From 734335281e4a73013dd650680bd403981429aef6 Mon Sep 17 00:00:00 2001 From: swarupsahu08 Date: Wed, 9 Oct 2024 19:07:10 +0530 Subject: [PATCH] yah the issue #3 is fixed --- main.html | 12 ++++++------ main.js | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/main.html b/main.html index d082400..4cb488e 100644 --- a/main.html +++ b/main.html @@ -16,11 +16,11 @@

Latest News Portal

@@ -34,6 +34,6 @@

Latest News Portal

© 2024 News Portal. All rights reserved. | Privacy Policy | Contact Us

- + diff --git a/main.js b/main.js index aaa5e34..c5dce0b 100644 --- a/main.js +++ b/main.js @@ -1,18 +1,22 @@ // Replace 'YOUR_API_KEY' with your actual NewsAPI key -const apiKey = 'YOUR_API_KEY'; -const apiUrl = `https://newsapi.org/v2/top-headlines?country=us&apiKey=${apiKey}`; +const apiKey = 'API_KEY'; +const bapiUrl = `https://newsapi.org/v2/top-headlines?apiKey=${apiKey}`; // Function to fetch news from the API -async function fetchNews() { +async function fetchCategoryNews(category) { const newsContainer = document.getElementById('news-container'); const loadingIndicator = document.getElementById('loading'); try { + loadingIndicator.style.display = 'block'; + + //It will build the url based on the selected category + const apiUrl = `${bapiUrl}&category=${category}&country=us`; // Fetch news from the API const response = await fetch(apiUrl); const data = await response.json(); - if (data.articles.length > 0) { + if (data.articles.length > 0 && data.articles) { // Remove loading indicator loadingIndicator.style.display = 'none'; @@ -38,4 +42,7 @@ async function fetchNews() { } // Fetch news on page load -document.addEventListener('DOMContentLoaded', fetchNews); +document.addEventListener('DOMContentLoaded',() => fetchCategoryNews('general')); + + +