Crating a random URL Button with custom Code

What’s good y’all,

I’ve got my site about finished. Would like my landing page to have a link (as you can see) that directs the user to a number of random pages to my site. Currently I have it set up in a very cheap way, where each letter is its own link, but I’d like it to truly be random and not based off which letter they click.
How might I go about doing this? Very determined to get this done. Would happily sacrifice the text link for a button underneath if that’s easier, I just want a button that is a randomizer essentially.

Here is my site Read-Only: (Webflow - Portfolio)
(how to share your site Read-Only link)

Hi @jacobhop3,

Welcome to the forum!

You can use some code like this in Page Settings > Custom Code > Before tag:


const links = [

let randomLink = links[Math.floor(Math.random() * links.length)];
const button = document.querySelector(".random-link");
button.addEventListener("click", function() {
    window.location.href = randomLink;


You’ll just need to wrap your this text in a span and give it the class 'random-link) for this to work. Quick demo here.

Edit: Sorry, I wrote ‘random-text’ instead of ‘random-link’ in the demo

Hope this helps!

Hey @jacobhop3 !

I think I’m getting this in just after @mww replied, but I figured I’d still throw a reply in anyways.

My answer is basically the same as Milan’s, but instead of using JavaScript to change window.location.href, I make the “let fate decide” text a link and change its href attribute to a random URL on click.

Here’s a video of me showing you how I did it:

And here’s the code I used:

  const urls = ["", "", ""];
  fateText = document.querySelector('.fate-text');
  fateText.addEventListener("click", function() {
  	let randomUrl = urls[Math.floor(Math.random() * urls.length)];
    fateText.setAttribute('href', randomUrl);

Either solution you go with should work great, though :slight_smile:

1 Like

Thank you! I’ll give this a shot and see how it shakes! I appreciate your help! :cowboy_hat_face:

I appreciate the help! Don’t trip, your explanation was just fine and I understand what I need to lol.

1 Like