Display a random description

Hi all,

I’ve put together a bit of JavaScript that will make the description of your blog one of a random list of strings (text blurbs) each time a page is viewed. This includes both the post list and a given post page.

Of course, this requires a paid subscription with write.as, since that’s the only way to allow inserting JavaScript into your blog.

BEFORE YOU USE: be aware that you must have a description set in your customization page. If you don’t, the description element where text will be displayed doesn’t get created, so the script will fail. If you have issues with this (or don’t want it for some reason), let me know as I should be able to alter the script to handle this. Whatever description you put will be over-written by this script, but I can’t guarantee it won’t appear briefly before this happens. It can be as simple as a single period or an ellipse or something, just so long as it’s there.

With that out of the way, all you have to do is copy the below code into the corresponding (i.e. JavaScript) section of your customization page. The list of descriptions you want to draw from is hopefully clear. Just be aware that each separate text entry must be enclosed in quotation marks and separated by commas (although the last one in the list doesn’t need a comma). If any entry includes an apostrophe, enclose it in double quotes ("), but otherwise either single or double will work (and you can mix and match).

Markdown or HTML within an option will not work, unfortunately, due to how the script operates.

const descrList = [ 'option 1', 'option 2', 'option 3', 'option 4' ];

const descr = document.getElementsByClassName('description')[0];
const theChosen = Math.floor(Math.random() * descrList.length);
descr.textContent = descrList[theChosen];

Please let me know if anything doesn’t work!

1 Like