random bgcolor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TaTonka

    random bgcolor

    hi!
    how can i manage it (html or jscript with css) that everytime a user loads
    or refreshes a page, the page has a new bgcolor.
    i want to put it in a single file, so that all my pages have the same color,
    but after every refresh a new random one.
    thanx 4 your support



  • Evertjan.

    #2
    Re: random bgcolor

    TaTonka wrote on 18 sep 2004 in comp.lang.javas cript:
    [color=blue]
    > how can i manage it (html or jscript with css) that everytime a user
    > loads or refreshes a page, the page has a new bgcolor.[/color]

    can be done

    <body onload='changeB gClr()'>
    [color=blue]
    > i want to put it in a single file, so that all my pages have the same
    > color, but after every refresh a new random one.[/color]

    This is illogical.
    You just asked for a new colour at every load or refresh.

    ========

    <script type='text/javascript'>

    function toHex(x) {
    switch(x) {
    case 10: return "A";
    case 11: return "B";
    case 12: return "C";
    case 13: return "D";
    case 14: return "E";
    case 15: return "F";
    }
    return x;
    }


    c='#'
    c+= toHex(Math.roun d(Math.random() *16));
    c+= toHex(Math.roun d(Math.random() *16));
    c+= toHex(Math.roun d(Math.random() *16));
    c+= toHex(Math.roun d(Math.random() *16));
    c+= toHex(Math.roun d(Math.random() *16));
    c+= toHex(Math.roun d(Math.random() *16));

    //alert(c)
    document.body.s tyle.background Color=c

    </script>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Dr John Stockton

      #3
      Re: random bgcolor

      JRS: In article <Xns9568B592FC1 FEeejj99@194.10 9.133.29>, dated Sat, 18
      Sep 2004 15:51:36, seen in news:comp.lang. javascript, Evertjan.
      <exjxw.hannivoo rt@interxnl.net > posted :[color=blue]
      >TaTonka wrote on 18 sep 2004 in comp.lang.javas cript:
      >[color=green]
      >> how can i manage it (html or jscript with css) that everytime a user
      >> loads or refreshes a page, the page has a new bgcolor.[/color]
      >
      >can be done
      >
      ><body onload='changeB gClr()'>
      >[color=green]
      >> i want to put it in a single file, so that all my pages have the same
      >> color, but after every refresh a new random one.[/color]
      >
      >This is illogical.
      >You just asked for a new colour at every load or refresh.
      >
      >========
      >
      ><script type='text/javascript'>
      >
      >function toHex(x) {
      > switch(x) {
      > case 10: return "A";
      > case 11: return "B";
      > case 12: return "C";
      > case 13: return "D";
      > case 14: return "E";
      > case 15: return "F";
      > }
      > return x;
      >}[/color]

      function toHex(x) { // uncomment one line; all untested
      // return String.fromChar Code( x>9 ? x+55 : x+48 )
      // return String.fromChar Code( x + [48,55][+(x>9)] )
      // return String.fromChar Code( x + 48 + 7*(x>9) )
      // return "0123456789ABCD EF".substr(x, 1)
      // return x.toString(16)
      }

      [color=blue]
      >c='#'
      >c+= toHex(Math.roun d(Math.random() *16));
      >c+= toHex(Math.roun d(Math.random() *16));
      >c+= toHex(Math.roun d(Math.random() *16));
      >c+= toHex(Math.roun d(Math.random() *16));
      >c+= toHex(Math.roun d(Math.random() *16));
      >c+= toHex(Math.roun d(Math.random() *16));[/color]

      You mean floor, not round; and there is the Opera bug that means that
      Math.random can give 1.0.

      The components can be concatenated in a single statement. But I'd use a
      loop.

      Or :

      c = ( "00000" + Math.floor( (Math.random()% 1)*Math.pow(16, 6) ).
      toString(16) ).substr(-6) // substr untested
      or

      c = ( "00000" + Math.floor( (Math.random()% 1)*Math.pow(16, 6) ).
      toString(16) ).replace(/(.*)(.{6})$/, '$2')
      or

      c = ( Math.pow(16,6) + Math.floor( (Math.random()% 1)*Math.pow(16, 6) ) ).
      toString(16).su bstring(1)

      or

      function Random(N) { return Math.floor(N*(M ath.random()%1) ) }

      function f() { var x = Random(16) ; return x + 48 + 7*(x>9) }

      c = String.fromChar Code(f(),f(),f( ),f(),f(),f())


      Remember to add the "#" !

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      • Evertjan.

        #4
        Re: random bgcolor

        Dr John Stockton wrote on 19 sep 2004 in comp.lang.javas cript:
        [color=blue]
        > You mean floor, not round;[/color]

        Yes, did not see that.
        [color=blue]
        > and there is the Opera bug that means that
        > Math.random can give 1.0.[/color]

        I prefer a musical for modus operandi.
        [color=blue]
        > The components can be concatenated in a single statement. But I'd use a
        > loop.[/color]

        I knew you would say that, John, and you are right.

        The question is if that would be as understandable to newbees.

        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress,
        but let us keep the discussions in the newsgroup)

        Comment

        Working...