-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
42 lines (31 loc) · 1.36 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
document.addEventListener('DOMContentLoaded', () => {
const memeContainer = document.getElementById('memeContainer');
async function fetchAndDisplayMeme() {
try {
const response = await fetch('https://meme-api.com/gimme');
if (!response.ok) {
throw new Error('Failed to fetch meme from the API');
}
const data = await response.json();
const memeUrl = data.url;
const memeImage = document.createElement('img');
memeImage.src = memeUrl;
memeImage.style.display = 'none';
memeImage.onload = () => {
const imageWidth = memeImage.naturalWidth;
const imageHeight = memeImage.naturalHeight;
memeContainer.style.width = `${imageWidth}px`;
memeContainer.style.height = `${imageHeight}px`;
memeImage.style.display = 'block';
memeImage.style.width = '100%';
memeImage.style.height = 'auto';
memeContainer.innerHTML = '';
memeContainer.appendChild(memeImage);
};
} catch (error) {
console.error('Error fetching meme:', error);
memeContainer.innerHTML = '<p>Failed to load meme. Please try again later.</p>';
}
}
fetchAndDisplayMeme();
});