mirror of
https://github.com/abrendan/smolNews.git
synced 2025-12-15 17:56:44 +01:00
Added error message when no articles found
This commit is contained in:
26
script.js
26
script.js
@@ -111,7 +111,21 @@ function fetchFromNYTAPI(query, topic) {
|
||||
|
||||
function displayArticles(articles, apiType) {
|
||||
const articlesDiv = document.getElementById('articles');
|
||||
articlesDiv.innerHTML = '';
|
||||
articlesDiv.innerHTML = ''; // Clear previous articles
|
||||
|
||||
const messageDiv = document.createElement('div'); // Create a message div
|
||||
|
||||
if (articles.length === 0) {
|
||||
// Display an error message if there are no articles
|
||||
messageDiv.innerHTML = '<p class="text-danger">No articles found. Please try a different search.</p>';
|
||||
messageDiv.className = 'message';
|
||||
document.body.appendChild(messageDiv);
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(messageDiv);
|
||||
}, 3000);
|
||||
return;
|
||||
}
|
||||
|
||||
articles.forEach(article => {
|
||||
const thumbnailUrl = article.image || 'default-thumbnail.jpg';
|
||||
|
||||
@@ -135,6 +149,16 @@ function displayArticles(articles, apiType) {
|
||||
`;
|
||||
articlesDiv.appendChild(articleElement);
|
||||
});
|
||||
|
||||
// Display success message
|
||||
messageDiv.innerHTML = '<p class="text-success">Articles loaded successfully!</p>';
|
||||
messageDiv.className = 'message';
|
||||
document.body.appendChild(messageDiv);
|
||||
|
||||
// Automatically remove the message after a few seconds
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(messageDiv);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
function viewArticleDetails(article) {
|
||||
|
||||
Reference in New Issue
Block a user