Create get-posts.js
Browse files- get-posts.js +35 -0
get-posts.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 2 |
+
const container = document.getElementById('posts-container');
|
| 3 |
+
|
| 4 |
+
fetch('sad.json')
|
| 5 |
+
.then(response => {
|
| 6 |
+
if (!response.ok) {
|
| 7 |
+
throw new Error('Failed to load sad.json');
|
| 8 |
+
}
|
| 9 |
+
return response.json();
|
| 10 |
+
})
|
| 11 |
+
.then(data => {
|
| 12 |
+
data.posts.forEach(post => {
|
| 13 |
+
const postElement = document.createElement('div');
|
| 14 |
+
postElement.classList.add('post');
|
| 15 |
+
|
| 16 |
+
const titleElement = document.createElement('h2');
|
| 17 |
+
titleElement.textContent = post['post-title'];
|
| 18 |
+
|
| 19 |
+
const fileElement = document.createElement('a');
|
| 20 |
+
fileElement.href = post.file;
|
| 21 |
+
fileElement.textContent = `${post.file}`;
|
| 22 |
+
|
| 23 |
+
postElement.appendChild(titleElement);
|
| 24 |
+
postElement.appendChild(fileElement);
|
| 25 |
+
|
| 26 |
+
container.appendChild(postElement);
|
| 27 |
+
});
|
| 28 |
+
})
|
| 29 |
+
.catch(error => {
|
| 30 |
+
console.error('Error loading posts:', error);
|
| 31 |
+
const errorElement = document.createElement('p');
|
| 32 |
+
errorElement.textContent = 'Failed to load posts. Check console for details.';
|
| 33 |
+
container.appendChild(errorElement);
|
| 34 |
+
});
|
| 35 |
+
});
|