Javascript: Random Link Generator

This is a very simple code that you can use to generate random links. You can create a button, link, image with this function, which will randomly choose between the specified URLs to visit.

The first thing would be to create the array with the URLs There are two common ways to do this: The condensed array:

Or the regular array:

You can use any of these methods to create the generator, the next step is to create the function that will generate random links using the javascript Math.random method.

And that’s it! All that’s left is to add the HTML portion of the code, it can be a button, image, link, etc. I will use a link for this example:

This code can be modified to do many different things. For example, if you would like to display random quotes on your website, you could modify this code a little bit to make that happen. I will create a separate tutorial for that, though. Here’s the complete code from this tutorial:

If you have any questions, don’t hesitate to leave a comment.

12 thoughts on “Javascript: Random Link Generator”

  1. hello!
    i have a text file with 100 links
    i want script that pick random link from text file that i uploaded on my site thankyou

    so every time i click on the button it picks a random link from the test file i uploded on site thankyou

    1. I suggest you use jQuery to do this. You can use the .get function, then use data.split to get each item from the text file and then apply a similar approach to display a random item. I will try to write a tutorial on this as soon as possible.

  2. Hey,
    Thanks for this.
    But how can i center/bottom the link button.

    or even better.

    what would the code be to desplay different random
    so you would press the button and in the window above it
    a random video would play.
    every time you press the button a different randome video would play above it


    1. You could try a similar approach using the video embed’s HTML in an array and display it that way. I will try to write a tutorial about this as soon as possible. I’ve added it on my to-do list.

    1. It’s basically the same way as the link, just change the array items to the image path/source you’d like to use. Then display the random function using the img tag. I will write a tutorial on this within the next few days. Thank you for your interest!

  3. Pingback: Javascript: Random Image Generator |

  4. Hello,

    I am looking to do a research survey however the design is a bit complex. Is there anyway to randomly assign urls twice? What I mean is to random assign 2 links, and then randomly assign 12 links from the first and 3 links from the second.

    The reason why it is much more complicated is because there are hence different % of acquiring the resultant 15 links. Is there anyway to do this?

  5. Hey!
    I have three websites and I want them to show up randomly, but I don’t want them to repeate. So once one is shown, I don’t want it to be shown again.
    Thanks in advanced!

  6. took some work – but your code was simple enough i could tweak it around and added the image- really happy – thanks – i would like to send you free art. artist R Mack

Leave a Comment

Your email address will not be published. Required fields are marked *