Random Color for Web Page Background

The simplest way to set a random color as a background is to generate a random color hash and set it using Css

So here is the code for that .

<html>
    <head>
<script type="text/javascript">// <![CDATA[
src</span>=”https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js”>
// ]]></script>
    <script>
        $(document).ready(function()
        {
            $(‘body’).css(‘background-color’,RandomColorHash());
        });
        function RandomColorHash()
        {
            var list=[ '0',  '1',  '2',  '3',  '4',  '5',  '6', '7',  '8', '9',  'A',  'B',  'C',  'D',  'E',  'F' ],Color=’#';
            for(var i=0;i<6;i++)
            {
                Color+=list[
                            Math.floor(
                                        Math.random()*list.length
                                        )
                            ];
            }
            return Color;
        }
    </script>
    </head>
    <body>
    </body>
</html>

The above code uses jQuery but this can also be done in JavaScript.

Advertisements