-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
29 lines (27 loc) · 881 Bytes
/
main.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
function getRandomPosInt(max) {
return Math.floor(Math.random() * Math.floor(max)) + 1;
}
function updatePage(xkcd) {
fetch('https://xkcd.com/' + xkcd + '/info.0.json').then(function(response) {
response.json().then(function(data) {
var num = data.num;
var img = data.img;
var title = data.safe_title;
var alt = data.alt;
var targetTitle = document.getElementById('title');
var targetImg = document.getElementById('xkcd');
var targetAlt = document.getElementById('alt');
targetImg.src = img;
targetTitle.href = "https://xkcd.com/" + num;
targetTitle.innerText = title;
targetAlt.textContent = alt;
});
});
}
fetch('https://xkcd.com/info.0.json').then(function(response) {
response.json().then(function(data) {
var num = data.num;
var x = getRandomPosInt(num);
updatePage(x);
});
});