levineuwirth.org/static/js/random.js

22 lines
792 B
JavaScript

/* random.js — "Random Page" button for the homepage.
Fetches /random-pages.json (essays + blog posts, generated at build time)
and navigates to a uniformly random entry on click. */
(function () {
'use strict';
document.addEventListener('DOMContentLoaded', function () {
var btn = document.getElementById('random-page-btn');
if (!btn) return;
btn.addEventListener('click', function () {
fetch('/random-pages.json')
.then(function (r) { return r.json(); })
.then(function (pages) {
if (!pages.length) return;
window.location.href = pages[Math.floor(Math.random() * pages.length)];
})
.catch(function () {});
});
});
}());