In a support thread titled “Can I specify random logos?”, AstonJ asks if it’s possible to randomly display one of three logos on each new session or browser refresh. Alankrit_Choudh suggests using JavaScript to randomly select and apply a logo image on hard refresh, providing sample code that modifies the CSS background of a logo element — though noting it only works reliably if placed before </head>. AstonJ thanks them and plans to test it. Michael_Smart confirms the solution fits his needs and asks for clarification on implementation. sam advises that while the JS approach can work, a better method is to use a custom Ember initializer within a theme component to override the logo URL directly in Discourse, avoiding reliance on window.onload. Gunnar echoes the issue with window.onload failing if executed too late (due to DOM elements not being ready), and requests beginner-friendly guidance on creating an Ember initializer for this purpose, sharing his own similar use case for rotating banner images. The discussion centers on balancing simplicity (JS) vs. robustness (Ember initializer) for dynamic logo/banners in Discourse.
Is it possible to (via a plug-in if necessary) randomly choose a logo (and small logo) from a set of 3 every time someone starts a new session (or until their browser refreshes the assets for the logo)?
The Ruby code is trivial - but how can we incorporate that into Discourse?
This is doable in a theme component, the method by @Alankrit_Choudh can work but I would recommend adding a custom ember initializer instead that overrides the URL in discourse instead of relying on window.onload here.
I’ve run up against the same problem. window.onload works if called before </head>, but not if called later. And because of that, document.getElementById fails (the ID isn’t defined yet).
Could you point me to a primer/example/howto for how to create a custom ember initializer for someone who has no experience with ember?
For completeness, what I’m trying to do is display a random banner image from an array of URLs:
var jlmyPix = new Array(
'/img/pic0.jpeg',
[ ... ]
'/img/picN.jpeg'
);
function jlchoosePic() {
var jlrandomPicNum = Math.floor(Math.random() * jlmyPix.length);
document.getElementById("jlmyPicture").src = jlmyPix[jlrandomPicNum];
};
window.onload = jlchoosePic;