Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Random colour

  1. #1
    Join Date
    Jan 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Random colour

    I'm making a new website & I want to know if it’s possible for the background colour to be randomly selected from a list of hex codes? So every time you go to the page there’s a different background colour.

    Thanks in advance.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script type="text/javascript" src="http://www.twey.co.uk/pythonic.js"></script>
    <script type="text/javascript">
      Twey.Pythonic.LOAD("Array.randomChoice");
      document.body.style.backgroundColor = "#" + [
        "fff",
        "000",
        "00f",
        "0ff",
        "0f0",
        "fff000"
      ].randomChoice();
    </script>
    Be careful that there's no text directly on top of this colour, though, or it might be unreadable sometimes. Also, don't forget users without scripting support -- always provide a default background colour or, if possible, generate the random colour server-side.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This cannot be done with just HTML.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    I don't think loading an external script is really worth the hassle for such a simple script.
    Having the script in the page eliminates the hotlinking to your own site and allows the coder to find out whats actually happening easier. (Twey, your external code is too sophisticated for begginers to understand.)


    Code:
    <script type="text/javascript">
    colors = new Array();
    colors[0] = "#FF0000";
    colors[1] = "#FFCC00";
    colors[2] = "#00FF00";
    colors[3] = "#0000FF";
    colors[4] = "#00FFCC";
    colors[5] = "#CCFF00";
    //etc
    
    randomNumber = Math.floor(Math.random()*colors.length);
    
    document.body.style.backgroundColor  = colors[randomNumber];
    </script>
    Last edited by Bob90; 05-20-2007 at 11:05 AM. Reason: added script type

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't think loading an external script is really worth the hassle for such a simple script.
    Mm, probably true.
    Having the script in the page eliminates the hotlinking to your own site and allows the coder to find out whats actually happening easier.
    They could always download it, or even copy-and-paste it into the script wholesale. It's only there for convenience.
    <script>
    The type attribute is required.
    colors = new Array();
    colors[0] = "#FF0000";
    colors[1] = "#FFCC00";
    colors[2] = "#00FF00";
    colors[3] = "#0000FF";
    colors[4] = "#00FFCC";
    colors[5] = "#CCFF00";
    That's a lot of assignments. I think it would be more efficient and certainly easier to use an array literal. There's also no need for all those globals; to avoid cluttering the global namespace, perhaps wrap it in a new scope:
    Code:
    <script type="text/javascript">
      (function() {
        var colours = [
          "#FF0000",
          "#FFCC00",
          "#00FF00",
          "#0000FF",
          "#00FFCC",
          "#CCFF00"
        ];
        document.body.style.backgroundColor = colours[Math.floor(Math.random() * colours.length)];
      })();
    </script>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Quote Originally Posted by Twey View Post
    Code:
    (function() {
        var colours = [
          "#FF0000",
          "#FFCC00",
          "#00FF00",
          "#0000FF",
          "#00FFCC",
          "#CCFF00"
        ];
        document.body.style.backgroundColor = colours[Math.floor(Math.random() * colours.length)];
      })();
    Just wondering, why are you using this anonymous function syntax?

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    So colours doesn't end up global. I'm not entirely sure if the cost of creating and running a function is worth it, but John reckon(s|ed) that it is.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    Very interesting points.
    I didn't think about global declaration, I was just supplying the level of solution I thought met the request (i.e new coder requesting something very easy; they obviously need an introduction to most things.)

  9. #9
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    So colours doesn't end up global. I'm not entirely sure if the cost of creating and running a function is worth it, but John reckon(s|ed) that it is.
    I see.
    Yet there's one "bug" in your code. If it's placed into the head section, then it's not gonna work (document.body...), however document.bgColor works fine.

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yet there's one "bug" in your code. If it's placed into the head section, then it's not gonna work (document.body...), however document.bgColor works fine.
    The bgColor property is deprecated for CSS. I was intending for it to be placed in the body, yes; I probably should have made that clearer.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •