JavaScript revelations

Update (Jan 17, 2023): I no longer use JavaScript on this website.

For a while I had a “now listening” widget on the homepage that displayed the song I last listened/was currently listening to by fetching data from my Last.fm account. The script I was using worked just fine but relied on jQuery, meaning I had to make visitors download two different files every time they visited my website and it got me thinking if there is a simpler, lighter way to do that.

And there is! It’s called fetch and unlike ajax it does not rely on any external libraries.

As someone who knows fuck all about JavaScript and would prefer to continue life in blissful ignorance of it, this is where multiple tutorial videos got me:

fetch(url)
    .then((response) => {
        return response.json();
    })
    .then((data) => {
        displaySong(data.recenttracks.track[0]);
    });

So begone jQuery! And hello to a simpler script.