Displaying the top JavaScript questions of the month from Stack Overflow's API
suggest changeThis browser example calls the Stack Exchange API and lists JavaScript questions ranked by the API's month sort (activity in the current month, not vote count). If the request fails, or the JSON reports error_message, the promise rejection path displays the error.
View live results on HyperWeb.
const url =
'https://api.stackexchange.com/2.2/questions?site=stackoverflow' +
'&tagged=javascript&sort=month&filter=unsafe&key=gik4BOCMC7J9doavgYteRw((';
fetch(url).then(response => response.json()).then(data => {
if (data.error_message) {
throw new Error(data.error_message);
}
const list = document.createElement('ol');
document.body.appendChild(list);
for (const {title, link} of data.items) {
const entry = document.createElement('li');
const hyperlink = document.createElement('a');
entry.appendChild(hyperlink);
list.appendChild(entry);
hyperlink.textContent = title;
hyperlink.href = link;
}
}).then(null, error => {
const message = document.createElement('pre');
document.body.appendChild(message);
message.style.color = 'red';
message.textContent = String(error);
});
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents