Can I specify random logos?

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? :confused:

You could do it with JS i suppose, here is code i hope it works

<script>
if (document.getElementById) { window.onload = swap };
function swap() {
var numimages=4;
rndimg = new Array("IMAGE URL 1", "IMAGE URL 2", "IMAGE URL 3", "IMAGE URL 4");
x=(Math.floor(Math.random()*numimages));
randomimage=(rndimg[x]);
document.getElementById("banner").style.logo-big = "url("+ randomimage +")";
}
</script>

PS this only works on hard refresh. also the css to big logo would be

.logo-big { 
background-image: url('DEFAULT LOGO IMAGE URL HERE');
}

The JS would work because i am using that to rotate my banner image randomly on every hard refresh not sure the exact code for logo CSS.

2 个赞

Thanks - I’ll give that a go :slight_smile:

Hi,

This is exactly what I need! Is the second part required? Also where does the first part get put exactly?

Best,
Michael

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.

1 个赞

Sam,

我也遇到了同样的问题。如果 window.onload</head> 之前调用,它可以正常工作,但如果调用时间较晚则不行。正因如此,document.getElementById 会失败(因为该 ID 尚未定义)。

能否为我提供一个入门指南、示例或操作教程,教一个没有 Ember 经验的人如何创建自定义的 Ember 初始化器?

为了完整起见,我试图实现的功能是从一组 URL 数组中随机显示一张横幅图片:

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;