Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7810633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:51:18+00:00 2026-06-02T03:51:18+00:00

Just wondering why my code isn’t working. Am I doing this completely wrong? window.onload

  • 0

Just wondering why my code isn’t working. Am I doing this completely wrong?

window.onload = function random() {
    var links = document.getElementsByTagName('a');
    var colours = new Array("green","red","blue");
    var randomColour = colours[Math.floor(colours.length * Math.random())];
    for (a = 0; a < links.length; a++) {
        links[a].style.color = 'randomColour';
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:51:19+00:00Added an answer on June 2, 2026 at 3:51 am

    'randomColor' -> randomColor.
    Use a variable instead of a quoted string. Also, to ensure that each link gets a random color, generate the color in the loop:

    for (var a = 0; a < links.length; a++) {
        var randomColour = colours[Math.floor(colours.length * Math.random())];
        links[a].style.color = randomColour;
    }
    

    To minimize the delay between setting the color, and parsing the link, bind a DOMContentLoaded event, or simply paste the code at the end of the <body>:

    http://jsfiddle.net/V6Chb/1/

    <a>Test</a>
      ... 
    <a>Test</a>
    <script>
    (function() { // <-- Anonymous function to not leak variables to the global scope
        var links = document.getElementsByTagName('a');
        var colours = ["green", "red", "blue"];
        for (var a = 0; a < links.length; a++) {
            var randomColour = colours[Math.floor(colours.length * Math.random())];
            links[a].style.color = randomColour;
        }
    })();  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just wondering if there is a best case to write this code: $('#set_duration_30').click(function(event)
Just wondering about this code below... when I turn off my internet connection and
Just wondering is this kind of code recommended to increase performance? void functionCalledLotsofTimes() {
This code isn't for anything in particular. I'm just trying to successfully get the
Hey, I just started wondering about this as I came upon a code that
This code snippet isn't working, I'm getting an Authentication Failed. response from the server.
This syntax for some reason isn't working and I'm wondering why. When I alert
Hey, I'm just wondering what code I could use to auto change the size
Just wondering, if I install a Windows service from 64-bit process (service code embedded
Some assertions in my code just started acting strangely, and I was wondering if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.