File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Programmer Humour</ title >
6+ < link rel ="stylesheet " href ="style.css ">
7+ </ head >
8+ < body >
9+
10+ < h1 > Latest XKCD Comic</ h1 >
11+
12+ < div id ="comic-container ">
13+ < p id ="loading "> Loading comic...</ p >
14+ </ div >
15+
16+ < script src ="script.js "> </ script >
17+ </ body >
18+ </ html >
Original file line number Diff line number Diff line change 1+ function fetchComic ( ) {
2+ const container = document . getElementById ( "comic-container" ) ;
3+ const loading = document . getElementById ( "loading" ) ;
4+
5+ fetch ( "https://xkcd.now.sh/?comic=latest" )
6+ . then ( response => {
7+ if ( ! response . ok ) {
8+ throw new Error ( "Network response was not ok" ) ;
9+ }
10+ return response . json ( ) ;
11+ } )
12+ . then ( data => {
13+ console . log ( data ) ;
14+
15+ if ( loading ) loading . remove ( ) ;
16+
17+ container . innerHTML = `
18+ <h2>${ data . title } </h2>
19+ <img src="${ data . img } " alt="${ data . alt } " />
20+ <p>${ data . alt } </p>
21+ ` ;
22+ } )
23+ . catch ( error => {
24+ console . error ( error ) ;
25+
26+ if ( loading ) {
27+ loading . textContent = "Error loading comic 😢" ;
28+ } else {
29+ container . innerHTML = `<p>Error loading comic 😢</p>` ;
30+ }
31+ } ) ;
32+ }
33+
34+ window . addEventListener ( "load" , fetchComic ) ;
Original file line number Diff line number Diff line change 1+ body {
2+ font-family : Arial, sans-serif;
3+ text-align : center;
4+ background-color : # f4f4f4 ;
5+ }
6+
7+ img {
8+ max-width : 100% ;
9+ height : auto;
10+ margin-top : 20px ;
11+ }
You can’t perform that action at this time.
0 commit comments